Manipulate related data
Manipulate child
Let us see how we can manipulate child entity from the parent entity
Create a child entity
Let us continue the same example; make the changes to program file in order to experiment the following commands. Below are the list of students
using EFGetStarted;
using System;
using System.Linq;
var db = new MyContext();
Console.WriteLine("List of students...\n");
foreach (var s in db.Students)
Console.WriteLine($" Id: {s.Id}\t Name: {s.Name} Is Enrolled: {s.IsEnrolled}");
Console.WriteLine();
Console.WriteLine("Press any key to exit");
Console.ReadKey();let us add a new vehicle entity
Let us create a new vehicle for student with Id 1 as follows
At this point, if you look at the database you should see a new row in the vehicles table as shown

Retrieve a child entity
Let us retrieve the same vehicle from the student reference as follows
(This will not work)
Manipulate parent
Retrieve parent entity
let us try to retrieve parent entity from child. Remember we have navigation properties for parent, in child definition!
replace line 7 with the following
Let us eager load the parent as well!
Delete parent entity
What happens if we try to delete the parent entity since there are children associated with it
Let's first create some records

Now let's delete the student Jack

Using navigation properties, we can deal with entities as if they were in memory collection of objects and not database records! This is the advantage of using an ORM framework like entity framework core! Of course, needless to mention the mapping of database column values on to object properties which happens behind the scenes!
Last updated