Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save detached entity in Entity Framework 6

I've read through LOTS of posts on saving a detached entity in Entity Framework. All of them seem to apply to older versions of Entity Framework. They reference methods such as ApplyCurrentValues and ChangeObjectState which do not seem to exist. On a whim I decided to try a method I found through intellisense and I want to make sure this is the correct way to do this since I don't get to see what happening behind the scenes:

public void SaveOrder(Order order) {     using (VirtualWebEntities db = new VirtualWebEntities())     {         db.Orders.Attach(order);         db.Entry(order).State = System.Data.Entity.EntityState.Modified;         db.SaveChanges();     } } 

Is this the correct way to update an existing item that was changed?

like image 724
KingOfHypocrites Avatar asked Dec 08 '13 08:12

KingOfHypocrites


People also ask

How do I detach an EF entity?

If you want to detach an object that is already attached to the context, set the state to Detached . If you want to load entities from the DB without attaching them at all to the context (no change tracking), use AsNoTracking . @kjbartel : this is the expected behavior, since the entity has no link with the context.

How do I save in Entity Framework?

Insert Data Add methods add a new entity to a context (instance of DbContext) which will insert a new record in the database when you call the SaveChanges() method. In the above example, context. Students. Add(std) adds a newly created instance of the Student entity to a context with Added EntityState.

What is detached state in Entity Framework?

Detached is the default state of a newly created entity because the context can't track the creation of any object in your code. This is true even if you instantiate the entity inside a using block of the context. Detached is even the state of entities retrieved from the database when tracking is disabled.

What's a recommended approach for using EF in disconnected applications?

Following are the two steps that needs to be taken with disconnected entity graph or even a single disconnected entity. Attach entities with the new context instance and make context aware about these entities. Set appropriate EntityStates to these entities manually.


1 Answers

Yes, this is correct. This article describes various ways of adding and attaching entities, and it provides this example:

var existingBlog = new Blog { BlogId = 1, Name = "ADO.NET Blog" }; using (var context = new BloggingContext()) {     // The next step implicitly attaches the entity     context.Entry(existingBlog).State = EntityState.Modified;     // Do some more work...     context.SaveChanges(); } 

Since EF doesn't know which properties are different from those in the database, it will update them all:

When you change the state to Modified all the properties of the entity will be marked as modified and all the property values will be sent to the database when SaveChanges is called.

To avoid this, you can set which properties are modified manually rather than setting the entire entity state:

using (var context = new BloggingContext()) {     var blog = context.Blogs.Find(1);     context.Entry(blog).Property(u => u.Name).IsModified = true;          // Use a string for the property name     context.Entry(blog).Property("Name").IsModified = true; } 
like image 77
Stephen Jennings Avatar answered Sep 19 '22 03:09

Stephen Jennings