Neo4jClient node ids

空扰寡人 提交于 2019-12-13 02:37:24

问题


I'm having trouble working out how the ID work in Neo4jClient. I want my .NET model to have an identifier property, and ideally I'd like it just to use the Neo4j autoincremented id for that node.

However, no matter what case I use (id, ID, Id) in my model class, it always adds this as another field in Neo4j (keeping it as 0 when I create a new node). So when I view that node in the Neo4j browser, it has a <id> which is auto-incremented, and also my id field which is always 0 (unless I manually set it in my model in C#).

I want to be able to create a new .NET model class (which will initially have an uninitialised id of 0), then once I've created it with the Neo4j fluent Cypher query, it will have the ID from the newly created node's autoincremented ID.

The examples here: https://github.com/Readify/Neo4jClient/wiki/cypher-examples

Show their User class of having an ID like this:

public long Id { get; set; }

But in the example for creating a new User ...

var newUser = new User { Id = 456, Name = "Jim" };
graphClient.Cypher
    .Create("(user:User {newUser})")
    .WithParam("newUser", newUser)
    .ExecuteWithoutResults();

I'm unsure where this 456 magic number comes from in this example, but I just want this to be the Neo4j id, which I obviously don't know until it's created.


回答1:


Alessandro is correct, and you shouldn't use the Node ID certainly not as maps to internal representations. If you were to delete a node, then create another, it may well have the same ID.

Now, there are some times you need to get the ID, (again not for use as an internal identifier) - but maybe in a Path result or something, and Neo4jClient does allow you to get it.

Be warned, this way dragons lie.

Neo4jClient is all about POCO's, it helps you translate them to and from Neo4j, the example from the WIKI is just that, an example, the ID could have come from any number of sources, or be any type, for example, I quite often use GUIDs for my IDs. Equally, I've used things like SnowMaker in the past to generate IDs. If you want the Node ID, you need to wrap your POCO in a Node<T> type, so:

client.Cypher.Match("(n:User)").Return(n => n.As<User>()).Results;

gets you an IEnumerable<User> response, whereas:

client.Cypher.Match("(n:User)").Return(n => n.As<Node<User>>()).Results;

gets you an IEnumerable<Node<User>> response, where each instance of Node<T> will have a property - Reference which is the Neo4j ID and another - Data which is the T / POCO bit.




回答2:


using id from Neo4j is a wrong practice since it may change over time or be assigned to a different node. Have a look at this plugin:

https://github.com/graphaware/neo4j-uuid

Cheers,



来源:https://stackoverflow.com/questions/33462504/neo4jclient-node-ids

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!