58

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?

1 Answer 1

92

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;
}
11
  • thanks. so i just want to clarify.. there is no longer a concept of applycurrentvalues as in previous versions where you could notify EF which fields changed? Dec 8, 2013 at 18:13
  • 1
    @KingOfHypocrites You can, I've edited my answer. I suggest looking through the EF6 documentation, it has lots of good examples and explanations, and is written in a very straightforward way. Dec 8, 2013 at 20:32
  • Thanks for link. The only thing with your example is that I would have to manually mark each property. The old version had an apply values method here you could take the new object and apply to the old one. This would automatically mark any changed properties so it will know what to update when saving. I'm still searching for an equivalent. Dec 8, 2013 at 20:44
  • 2
    @KingOfHypocrites The second article I linked ("Working with property values") shows several ways of setting properties using other objects and dictionaries, including entity.CurrentValues.Set(order). I think this is what what you're looking for. If you start by attaching an entity, it begins as EntityState.Unmodified. Then set the properties using whatever method you choose. The entity will be flagged as Modified and just those properties changed will be updated. Dec 8, 2013 at 22:07
  • 1
    @NiklausWirth If you modify an OrderLine entity, you should mark the OrderLine entity as modified instead of marking the navigational property on the parent entity. Aug 17, 2014 at 23:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.