In DisConnected Scenario, following methods has to be attached to the context if wanted to do CRUD Operations on the DisConnected Scenario.
DbSet.Add()
DbSet.Attach()
Dbset.Entry()
DbSet.Add()
Description
DbSet.Add() method will attach a new context and added entity state to all the entities in the context.
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()
Description
DbSet.Attach() method is used to attach a new context and unchanged state for all the entities in the context.
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()
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.