简体   繁体   中英

Neo4jClient node ids

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.

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 GUID s 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.

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,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM