Connected And Disconnected Scenarios in Entity Framework

Introduction

 
There are 2 ways (connected and disconnected) to connect to and work with the Entity Framework. Both ways have their own importance. Let's suppose we want to display some data in a Presentation Layer and we are using some n-tier application, so it would be better to open the context, fetch the data and finally close the context. Since here we have fetched the data and closed the context the entities that we have fetched are no longer tracked and this is the disconnected scenario. For a connected scenario let us suppose we have a Windows service and we are doing some business operations with that entity so we will open the context, loop through all the entities, do our business operations and then save the changes with the same context that we opened in the beginning. In the case of a connected scenario, the changes are tracked by the context but in the case of a disconnected scenario, we need to inform the context about the state of the entity.
 
Practical Explanation
 
Let's create a table named Employee in a database as in the following:
  1. CREATE TABLE [dbo].[Employee]  
  2. (    
  3.   [EmployeeID] [int] IDENTITY(1,1) NOT NULL,[EmployeeName] [varchar](50) NULL,  
  4.   CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED ( [EmployeeID] ASC )  
  5.   WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]    
  6. ON [PRIMARY]    
After generating a .edmx of the corresponding table will look like:
 
 
So now in the connected scenario, let us suppose we want to get all the employees from the database and want to change their name. So the code would look like:
 
  • Line 1 : We opened the context.
  • Line 2: We got all the employees in the database.
  • Line 3: We are editing the employee name.
  • Line 4: We are saving changes to the database.
Again, since we have opened the context and we are editing the entities, the context is tracking the changes so we need not provide the changing state explicitly here.
 
Let's look at the disconnected scenario where we have fetched a specific employee entity. The context is closed, from the Presentation Layer we will change the entity and will send the changed entity to be saved in the database. So the update code will look like:
 
  • Line 1 : We have opened the context.
  • Line 2 : We have attached the disconnected entity to the context.
  • Line 3 : We need to explicitly define the state.
  • Line 4 : Finally go ahead and save the changes. (This will update the entity.)
Since we are discussing the entity state here, there can be five entity states for every entity, and depending upon which Entity Framework decides the operation when saving changes is called. The five entity states are as in the following:
 
 
Similarly for deleting an entity in the disconnected scenario, the code will look like:
 
  • Line 1: We have opened the context.
  • Line 2: We have attached the disconnected entity to the context.
  • Line 3: We need to explicitly define the state.
  • Line 4: Finally go ahead and save the changes. (This will delete the entity.)