-
Notifications
You must be signed in to change notification settings - Fork 5
Creating nodes
To create new nodes, it is simple. After defining the configuration for your nodes, a new node can be added by simply accessing the NodeSet<>
and call the Add
or AddRange
commands as follow.
graphContext.Movies.Add(newMovie);
await graphContext.SaveChangesAsync();
graphContext.Movies.AddRange(newMovies);
await graphContext.SaveChangesAsync();
graphContext.Movies.Add(newMovie);
graphContext.People.Add(newPerson);
await graphContext.SaveChangesAsync();
The SaveChangesAsync
will run all the creation methods in one transaction against the database.
When the newMovie
object has some actors in it too, the SaveChanges
will try to merge
those actors with their relations as well. In neo4j MERGE
means MATCH
or CREATE
. So if you want to make sure that you are matching correctly and are not planning to create a new node. In actor just specify the Id
of the actor. If neo4j finds a node with that specifications it will only do a match.
Good to know: The default Isolation Level
in neo4j is read-committed
.
After v1.1.0 it is possible to add new nodes to the database without knowing their types explicitly in C#.
⚠️ IMPORTANT: Working with anonymous nodes have the risk to be vulnerable against cypher injection. Because you need to pass a string labelGraphContext.Anonymous("Label")
to theAnonymous
method in order to create a generic NodeSet on the fly and this label will be used to build the cypher. Now if theLabel
is replaced with e.g.var label = "Movie { Id: 1, Name: 'Test' }); MATCH(a) detach delete a //"Then the normal cypher will be ignored and the database will be wiped out. This means it is IMPORTANT to validate the received label if it is coming from an untrusted source. More info about cypher injection
To create an anonymous node, you can do the following:
graphContext.Anonymous("Movie").Add(anonymousMovieObject);
graphContext.Anonymous("Person").Add(anonymousPersonObject);
graphContext.SaveChanges();
The anonymous object must be a Dictionary<string, object>
with following specs:
- The value types like
Int
,String
,Bool
,DateTime
, etc can be simply assigned to a key in the Dictionary. - If the value is an object, you need to convert that one also to a
Dictionary<string, object>
with all specifications being mentioned here. - If the value is a list of objects, you need to convert the objects inside the list to
Dictionary<string, object>
.