Entity Framework - SPLessons

Entity Framework Add Entity Graph

Home > Lesson > Chapter 16
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Entity Framework Add Entity Graph

Entity Framework Add Entity Graph

Entity Graph

shape Description

Entity Graph means the relation between the entities. The type of relation may be anyone. That means One to One, One to Many..etc

shape Example

Employee relation with the Departments is an example for Entity graph.In Disconnected Scenario, add Dbset.Add() method in order to attach the entity graph to the context. The following code will add Employee Entity Graph in the disconnected scenario.

shape Example

[csharp] //Create Employee in disconnected mode Employee newEmployee = new Employee () { Employee Name = "New Single Employee " }; //Assign new standard to Employee entity newEmployee .Standard = new Standard() { StandardName = "New Standard" }; //add new Department with new Department into Employee.departments newEmployee.Departments.Add(new Department() { DepartmentName = "New Department for single Employee", Department= new Department() { EmployeeName = "New Department" } }); using (var con= new EmployeeDBEntities()) { con.Employee.Add(newEmployee); con.SaveChanges(); Console.WriteLine("New Employee Entity has been added with new EmployeeId= " + newEmployee.EmployeeID.ToString()); Console.WriteLine("New Standard Entity has been added with new StandardId= " + newEmployee.StandardId.ToString()); Console.WriteLine("New Department Entity has been added with newDepartmentId= " + newStudent.Departments.ElementAt(0).DepartmentId.ToString()); Console.WriteLine("New DepartmentEntity has been added with new DepartmentId= " + newEmployee.Departments.ElementAt(0).DepartmentId.ToString()); } [/csharp]