在Neo4jclient中使用变异密码更新整个节点
本文关键字:更新 节点 密码 变异 Neo4jclient | 更新日期: 2023-09-27 17:53:56
我需要使用变异密码更新给定节点的所有属性。我想远离Node和NodeReference,因为我知道它们被弃用了,所以不能使用IGraphClient.Update。我对变异密码还是个新手。我正在用c#编写,使用Neo4jclient作为Neo4j的接口。
我做了以下代码,更新了"resunit"的"Name"属性,其中属性"UniqueId"等于2。这很好。然而,
*我的reunit对象有很多属性
我不知道哪些属性改变了
*我正在尝试编写代码,将工作与不同类型的对象(具有不同的属性)
这是可能的与IGraphClient。更新来传递整个对象,它将负责创建设置所有属性的密码。
我能以某种方式传递我的对象与变异密码,以及?我能看到的唯一替代方案是反射对象以找到所有属性并为每个属性生成. set,这是我想避免的。如果我走错了方向,请告诉我。
string newName = "A welcoming home";
var query2 = agencyDataAccessor
.GetAgencyByKey(requestingUser.AgencyKey)
.Match("(agency)-[:HAS_RESUNIT_NODE]->(categoryResUnitNode)-[:THE_UNIT_NODE]->(resunit)")
.Where("resunit.UniqueId = {uniqueId}")
.WithParams(new { uniqueId = 2 })
.With("resunit")
.Set("resunit.Name = {residentialUnitName}")
.WithParams(new { residentialUnitName = newName });
query2.ExecuteWithoutResults();
确实可以传递整个对象!下面我有一个名为Thing
的对象,定义如下:
public class Thing
{
public int Id { get; set; }
public string Value { get; set; }
public DateTimeOffset Date { get; set; }
public int AnInt { get; set; }
}
然后下面的代码创建一个新的Thing
并将其插入到DB中,然后通过使用一个Set
命令获取它并更新它:
Thing thing = new Thing{AnInt = 12, Date = new DateTimeOffset(DateTime.Now), Value = "Foo", Id = 1};
gc.Cypher
.Create("(n:Test {thingParam})")
.WithParam("thingParam", thing)
.ExecuteWithoutResults();
var thingRes = gc.Cypher.Match("(n:Test)").Where((Thing n) => n.Id == 1).Return(n => n.As<Thing>()).Results.Single();
Console.WriteLine("Found: {0},{1},{2},{3}", thingRes.Id, thingRes.Value, thingRes.AnInt, thingRes.Date);
thingRes.AnInt += 100;
thingRes.Value = "Bar";
thingRes.Date = thingRes.Date.AddMonths(1);
gc.Cypher
.Match("(n:Test)")
.Where((Thing n) => n.Id == 1)
.Set("n = {thingParam}")
.WithParam("thingParam", thingRes)
.ExecuteWithoutResults();
var thingRes2 = gc.Cypher.Match("(n:Test)").Where((Thing n) => n.Id == 1).Return(n => n.As<Thing>()).Results.Single();
Console.WriteLine("Found: {0},{1},{2},{3}", thingRes2.Id, thingRes2.Value, thingRes2.AnInt, thingRes2.Date);
给了:
Found: 1,Foo,12,2014-03-27 15:37:49 +00:00
Found: 1,Bar,112,2014-04-27 15:37:49 +00:00
所有属性都更新了!