• Skip to content
  • Skip to primary sidebar
  • Home
  • Angular
  • ASP.NET Core
  • Entity Framework
    • Entity Framework 6
    • Entity Framework Core
  • Crystal Reports
  • C#
  • ASP.NET
  • About Us
    • Privacy Policy
    • Contact Us

TekTutorialsHub

Free Online Tutorials

Update Record in Entity Framework

August 4, 2016 by TekTutorialsHub Leave a Comment

Adding New Entities
Deleting Records
 

In this tutorial let us look at how to update an existing entity in the database. We will also look at how to update the entity in both Connected and Disconnected states. You can look at our tutorial how to Add entities in Entity Framework.

In this article

  • Updating the Entity
    • Updating in Connected Scenario
    • Updating in Disconnected Scenario

Updating the Entity

Updating the entity involves getting the entity from the database, make the necessary changes and then call the SaveChanges to persist the changes in the database.

In one of our tutorial, we discussed how Persistence is handled in Entity Framework. Two scenarios are possible when we update and entity. One is Connected and the other one is the Disconnected scenario. Let us look at both Scenarios.

Updating in Connected Scenario

Let us modify the Descr field of Accounts Department in Connected Scenario. First, we create a Context (EFContext). Then, we retrieve the existing department data from the database. Then, Modify the Descr field and then call the SaveChanges to update the changes to the database.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
Department dep;
 
//Connected Scenario
using (EFContext db = new EFContext())
{
    db.Database.Log = Console.WriteLine;
 
    dep = db.Departments.Where(d=>d.Name=="Accounts").First();
    dep.Descr = "This is Accounts Department";
    db.SaveChanges();
 
    Console.WriteLine("Department {0} ({1}) is modified", dep.Name, dep.DepartmentID);
    Console.ReadKey();
}
 

The above code runs in the Connected Scenario. In Connected Scenario, the context detects the changes made to the department entity. It changes it state to Modified. When SaveCharges is called Context automatically creates an update query and persists the changes to the database

Updating in Disconnected Scenario

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
Department dep;
 
//Disconnected Scenario
using (EFContext db = new EFContext())
{
    Console.Clear();
    db.Database.Log = Console.WriteLine;
    dep = db.Departments.Where(d => d.Name == "Accounts").First();
}
 
dep.Descr = "Accounts Department-Disconnected Scenario";
using (EFContext db = new EFContext())
{
    db.Database.Log = Console.WriteLine;
    db.Departments.Add(dep);
    db.Entry(dep).State = System.Data.Entity.EntityState.Modified;
    db.SaveChanges();
}
 

In the above example, the context in which department is fetched is destroyed. The Descr field of the dep object is modified outside the context scope. Then the new context is created and Dep object is added to the context via DBset.Add method.

The new context is created is unaware of the changes made to the Dep object. Now we need to tell the context that the Entity is Updated. This is done using the Entry method, which exposes the State Property and changing its state as Modified. The SaveChanges method issues update query to update the database.

Whenever you add an Entity to DBSet, the context marks the State of Entity as Added. We discussed this in our previous tutorial Adding Entities in Entity Framework. Hence, in the above code, when the dep object is added to the DBSet it’s State is Set to Added. If we did not change its state to Modify and invoked the SaveChanges method, then the Context will insert the new department in the database. This will result in duplicate Departments in our database.

Adding New Entities
Deleting Records
 

Filed Under: Entity Framework

Leave a Reply

wpdiscuz_captcharefresh
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.
wpdiscuz_captcharefresh
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of

Primary Sidebar

Copyright ©2008-2018

About us Contact Privacy Policy

Feb,22,2019 12:18:17 PM

Copyright © 2019 · Magazine Pro on Genesis Framework · WordPress · Log in

wpDiscuz
Our web site uses cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.OkRead more