-
Notifications
You must be signed in to change notification settings - Fork 8
Blueprint41 CRUD
The following steps will guide you on how to create, read, update and delete an instance of Movie
and Person
entity.
Before your application can access the database using Blueprint41, you need to let it know where the database is, and what model of data that is stored in it.
-
In the
Main
method, set up the database connection, specify the database server and its authentication credentials.Driver.Neo4jPersistenceProvider provider = new Driver.Neo4jPersistenceProvider("bolt://localhost:7687", "[USERNAME_HERE]", "[PASSWORD_HERE]", "[OPTIONAL_DATABASE_NAME]"); PersistenceProvider.CurrentPersistenceProvider = provider;
-
In the
Main
method, create an instance of your datastore modelDataStore
and call theExecute
method to perform a database upgrade if it already exists or creates the database if it's not.Datastore model = new Datastore(); model.Execute(true);
You've successfully set up your database and the database model. Next, you will create data for the Movie
and Person
entity.
-
In the file
Program.cs
, create a static methodCreateMoviesAndActors
and in that method, create a transaction and instantiate the new objects for theMovie
andPerson
entity.private static void CreateMoviesAndActors() { using (Transaction.Begin(true)) { Movie matrix = new Movie() { title = "The Matrix", tagline = "Welcome to the Real World", released = 1999 }; Movie johnWick = new Movie() { title = "John Wick", tagline = "Revenge is all he has left", released = 2014 }; Movie toyStory = new Movie() { title = "Toy Story 4", tagline = "Get Ready To Hit The Road.", released = 2019 }; Movie cloudAtlas = new Movie() { title = "Cloud Atlas", tagline = "Everything is connected", released = 2012 }; Person keanu = new Person() { name = "Keanu Reeves", born = 1964 }; Person laurence = new Person() { name = "Laurence Fishburne", born = 1961 }; Person tom = new Person() { name = "Tom Hanks", born = 1956 }; Transaction.Commit(); } }
Note
Always commit the transaction to persist the objects created.
Important
When having database changes, you should use the Transaction.Begin(true)
else Transaction.Begin()
when just having a query.
Add missing using
directive.
-
In the
CreateMoviesAndActors
method, copy the code below and paste it before the lineTransaction.Commit()
.matrix.AddActor(keanu, roles: new string[] { "Thomas Anderson" }); matrix.AddActor(laurence, roles: new string[] { "Morpheus" }); matrix.AddGenre(Genre.Load(Genre.StaticData.Name.Action)); matrix.AddGenre(Genre.Load(Genre.StaticData.Name.Sci_Fi)); tom.AddMovieReview(matrix, rating: 5, summary: "Nice Movie! Excellent!"); keanu.AddFollower(laurence); johnWick.AddActor(keanu, roles: new string[] { "John Wick" }); johnWick.AddGenre(Genre.Load(Genre.StaticData.Name.Action)); cloudAtlas.AddActor(tom, roles: new string[] { "Dr. Henry Goose", "Hotel Manager", "Isaac Sachs", "Dermot Hoggins", "Zachry" }); cloudAtlas.AddGenre(Genre.Load(Genre.StaticData.Name.Sci_Fi)); cloudAtlas.AddGenre(Genre.Load(Genre.StaticData.Name.Drama)); toyStory.AddActor(tom, roles: new string[] { "Woody (voice)" }); toyStory.AddActor(keanu, roles: new string[] { "Duke Caboom (voice)" }); toyStory.AddGenre(Genre.Load(Genre.StaticData.Name.Animation));
This code will add
Person
keanu
andlaurence
asActors
on the"The Matrix"
movie, andtom
on"Cloud Atlas"
movie. TheMovie
Matrix
will also haveAction
andSci-Fi
genres;Sci-Fi
for"Cloud Atlas"
Movie
. -
In the
Main
method, call theCreateMoviesAndActors
method.static void Main(string[] args) { Driver.Neo4jPersistenceProvider provider = new Driver.Neo4jPersistenceProvider("bolt://localhost:7687", "[USERNAME_HERE]", "[PASSWORD_HERE]"); PersistenceProvider.CurrentPersistenceProvider = provider; Datastore model = new Datastore(); model.Execute(true); CreateMoviesAndActors(); }
-
Run the "MovieGraph" application. Select Debug > Start Debugging or press F5.
You have successfully created records of the Person
, Movie
and Genre
entity.
Note
After creating the records, in the Main
method, comment the line where the method CreateMoviesAndActors
is called so that it won't create duplicate records.
-
To display the
Movie
records with its properties (title
,tagline
,released
),Genres
and it'sActors
. Create the static methodGetMoviesAndTheActors
. Then in that method, create a transaction and use theMovie.GetAll()
method to get allMovie
records.private static void GetMoviesAndTheActors() { using (Transaction.Begin()) { foreach (Movie movie in Movie.GetAll()) { Console.WriteLine($"Movie: { movie.title }, Tagline: { movie.tagline }, Released: { movie.released }"); // Get Genres foreach (Genre genre in movie.Genres) { Console.WriteLine($" Genre: { genre.Name }"); } // Get Actors foreach (Person actor in movie.Actors) { Console.WriteLine($" Actor: { actor.name }, Born: { actor.born }"); } } //No commit needed, since we're only reading anyway... } }
-
In the
Main
method, call theGetMoviesAndTheActors
method after theCreateMoviesAndActors
method and perform theConsole.Read
method so that you can see the message in the console.static void Main(string[] args) { Driver.Neo4jPersistenceProvider provider = new Driver.Neo4jPersistenceProvider("bolt://localhost:7687", "[USERNAME_HERE]", "[PASSWORD_HERE]"); PersistenceProvider.CurrentPersistenceProvider = provider; Datastore model = new Datastore(); model.Execute(true); //CreateMoviesAndActors(); GetMoviesAndTheActors(); Console.ReadKey(); }
-
Press F5 to run the "MovieGraph" application.
After you've successfully displayed all the Movie
records, next steps will guide you on how to delete a Movie
record.
-
To delete the
Movie
record, create the static methodDeleteMovie(string)
. In the method, create a transaction and get theMovie
record by calling theMovie.LoadBytitle(string)
method and setmovieTitle
parameter value to"Cloud Atlas"
. After getting the record, make sure to remove the relationships forActors
andGenres
first. Use the methodmovie.Delete()
to delete the record.private static void DeleteMovie(string movieTitle) { using (Transaction.Begin(true)) { Movie movie = Movie.LoadBytitle(movieTitle); if (movie is not null) { movie.Actors.Clear(); movie.Genres.Clear(); movie.Delete(); Console.WriteLine($@"Movie ""{ movieTitle }"" successfully deleted."); } Transaction.Commit(); } }
-
In the
Main
method, call theDeleteMovie
method after theCreateMoviesAndActors
method and before theGetMoviesAndTheActors
method.
Note
Comment the line where the method GetMoviesAndTheActors
is called so that it won't display the other movie records.
static void Main(string[] args)
{
Driver.Neo4jPersistenceProvider provider = new Driver.Neo4jPersistenceProvider("bolt://localhost:7687", "[USERNAME_HERE]", "[PASSWORD_HERE]");
PersistenceProvider.CurrentPersistenceProvider = provider;
Datastore model = new Datastore();
model.Execute(true);
//CreateMoviesAndActors();
DeleteMovie("Cloud Atlas");
//GetMoviesAndTheActors();
Console.ReadKey();
}
- Press F5 to run the "MovieGraph" application.
You've successfully deleted the Movie
"Cloud Atlas"
. Next is to update a Movie
record.
-
To update a
Movie
record, create the static methodUpdateMovie
. In the method, create a transaction and get theMovie
record by calling theMovie.LoadBytitle(string)
method and settitle
parameter value to"The Matrix"
. Change the values of the following properties:Property Value title The Matrix Reload tagline Reload before the revolution begins. released 2003 private static void UpdateMovie() { using (Transaction.Begin(true)) { Movie movie = Movie.LoadBytitle("The Matrix"); if (movie is not null) { movie.title = "The Matrix Reload"; movie.tagline = "Reload before the revolution begins."; movie.released = 2003; } Transaction.Commit(); } }
-
In the
Main
method, call theUpdateMovie
method after theDeleteMovie
method and before theGetMoviesAndTheActors
method.static void Main(string[] args) { Driver.Neo4jPersistenceProvider provider = new Driver.Neo4jPersistenceProvider("bolt://localhost:7687", "[USERNAME_HERE]", "[PASSWORD_HERE]"); PersistenceProvider.CurrentPersistenceProvider = provider; Datastore model = new Datastore(); model.Execute(true); //CreateMoviesAndActors(); //DeleteMovie("Cloud Atlas"); UpdateMovie(); GetMoviesAndTheActors(); Console.ReadKey(); }
-
Press F5 to run the "MovieGraph" application.
🎉 You've successfully created, read, updated and deleted a Movie
record. Next topic, we will discuss on how to load and update a relationship.