Entity Framework - Disconnected Entities


In Disconnected scenario an entity is retrieved from the database and modified in the different context. Disconnected scenario is complex because context doesn’t know anything about modified entity so you have to tell the ObjectContext that what has changed in entity.

Entity Framework API provides some important methods that attaches disconnected entities to the new context and also set EntityStates to all the entities of an entity graph. Two work with disconnected entities, we have to follow two stepes. First, need to attach entities with the new context and make context aware about these entities. Second, set appropriate EntityStates to these entities manually because the new context instance does not know anything about the operations performed on the disconnected entities, so the new context cannot apply the appropriate EntityState.

DbSet.Add()
DbSet.Add() method attaches the complete entity graph to the new context and automatically applies Added entity state to all the entities (Parent and Child).

//Disconnected entity 
Employee disconnectedEmp = new Employee() { Name = "New Emp" };
disconnectedEmp.EmployeeAddress = new EmployeeAddress() { Address = "Address1", City = "City1" };

using (var ctx = new EFSourceDBEntities())
{
  //Add disconnected employee entity graph to new context instance - ctx

  ctx.Employees.Add(disconnectedEmp);

  //Get DbEntityEntry instance to check the EntityState of specified entity
  var empEntry = ctx.Entry(disconnectedEmp);
  var addressEntry = ctx.Entry(disconnectedEmp.EmployeeAddress);

  //Output
  Console.WriteLine("EntityState - Employee: {0}",empEntry.State);
  Console.WriteLine("EntityState - EmployeeAddress: {0}",addressEntry.State);
}

DbSet.Attach()
DbSet.Attach method attache an entire entity graph to the new context with Unchanged entity state.
We can attach disconnected entity graph using DbSet.Attach method. Attach method will only attach entity graph to the context, so we have to find the appropriate entity state of each entity.

//Disconnected entity
Employee disconnectedEmp = new Employee() { Name = "New Emp" };
disconnectedEmp.EmployeeAddress = new EmployeeAddress() { Address = "Address1", City = "City1" };

using (var ctx = new EFSourceDBEntities())
{
  //Attach disconnected employee entity graph to new context instance

  ctx.Employees.Attach(disconnectedEmp);

  //Get DbEntityEntry instance to check the EntityState of specified entity
  var empEntry = ctx.Entry(disconnectedEmp);
  var addressEntry = ctx.Entry(disconnectedEmp.EmployeeAddress);

  // Output
  Console.WriteLine("EntityState - Employee: {0}",empEntry.State);
  Console.WriteLine("EntityState - EmployeeAddress: {0}",addressEntry.State);
}

DbContext.Entry()
DbContext Entry method returns DbEntityEntry instance for a specified entity. DbEntityEntry also used to change the state of an entity. This method attaches a whole entity graph to the context with specified state to the parent entity and set the state of other(child) entities.

DbContext.Entry(disconnectedEntity).state = EntityState.Added/Modified/Deleted/Unchanged
 

Praesent mattis

Pellentesque viverra vulputate enim. Aliquam erat volutpat. Pellentesque tristique ante ut risus. Quisque dictum. Integer nisl risus, sagittis convallis, rutrum id, elementum congue, nibh. Suspendisse dictum porta lectus. Donec placerat odio vel elit.

Read More