Entity Framework - SPLessons

Entity Framework DisConnected Scenario

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

Entity Framework DisConnected Scenario

DisConnected Scenario

shape Description

In DisConnected Scenario, following methods has to be attached to the context if wanted to do CRUD Operations on the DisConnected Scenario.

DbSet.Add()

shape Description

DbSet.Add() method will attach a new context and added entity state to all the entities in the context.

shape Example

[csharp] Employee disconnectedEmployee= new Employee() { Name = "New Employee" }; disconnectedEmployee.Salary = new Salary() { Salary1 = "Salary" }; using (var con= new EmployeeDBEntity()) { //add disconnected employee entity graph to new context instance - ctx con.Employee.Add(disconnectedEmployee); // get DbEntityEntry instance to check the EntityState of specified entity var EmployeeEntry = con.Entry(disconnectedEmployee); var salaryEntry = con.Entry(disconnectedStudent.Salary); Console.WriteLine("Employee EntityState: {0}",EmployeeEntry.State); Console.WriteLine("Salary EntityState: {0}",salaryEntry.State); }[/csharp]

DbSet.Attach()

shape Description

DbSet.Attach() method is used to attach a new context and unchanged state for all the entities in the context.

shape Example

[csharp] Employee disconnectedEmployee= new Employee() { Name = "New Employee" }; disconnectedEmployee.Salary = new Salary() { Salary1 = "Salary" }; using (var con= new EmployeeDBEntity()) { //add disconnected employee entity graph to new context instance - ctx con.Employee.Attach(disconnectedEmployee); // get DbEntityEntry instance to check the EntityState of specified entity var EmployeeEntry = con.Entry(disconnectedEmployee); var salaryEntry = con.Entry(disconnectedStudent.Salary); Console.WriteLine("Employee EntityState: {0}",EmployeeEntry.State); Console.WriteLine("Salary EntityState: {0}",salaryEntry.State); }[/csharp]

DbSet.Entry()

shape Description

DbSet.Entry() method is used to attach a new context and Entry method is used to change the state for all the entities in the context.