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?

KingOfHypocrites
  • 9,316
  • 9
  • 47
  • 69

1 Answers1

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;
}
Stephen Jennings
  • 12,494
  • 5
  • 47
  • 66
  • 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? – KingOfHypocrites Dec 08 '13 at 18:13
  • 1
    @KingOfHypocrites You can, I've edited my answer. I suggest looking through the [EF6 documentation](http://msdn.microsoft.com/en-us/data/ee712907), it has lots of good examples and explanations, and is written in a very straightforward way. – Stephen Jennings Dec 08 '13 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. – KingOfHypocrites Dec 08 '13 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. – Stephen Jennings Dec 08 '13 at 22:07
  • Hey, i am facing the same problem and have applied the solution provided but it doesn't work for me. My code goes below. DataSource.Questions.Attach(QuestionEnt); DataSource.Entry(QuestionEnt).State = System.Data.EntityState.Modified; DataSource.SaveChanges(); – LojiSmith Jul 21 '14 at 12:23
  • But does that manner work for OrderLines of order object if they were updated? – Niklaus Wirth Aug 17 '14 at 11:45
  • 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. – Stephen Jennings Aug 17 '14 at 23:25
  • I was searching for a way to save detached objects and I came along to this page. I have managed to do it acting in a list with AsNoTracking (i.e.: `context.Set().AsNoTracking()`;), then I have changed object state with `context.Entry(obj).State = EntityState.Modified;` and could `SaveChanges();` This will allow anyone who tries to save a pair of unique keys within a database table that does not have autoincrement. – Junior Mayhé Sep 06 '14 at 19:48
  • Is anybody aware of a way to set the modified properties using AutoMapper? For instance, let's say I have a view model representing only a subset of data in my graph of affected entities. Can I use AutoMapper to mark only the fields represented by the view model as modified? – CShark Oct 15 '14 at 07:03
  • @CraftBeerHipsterDude yes, but it wouldn't rely on AutoMapper. If your models implement 'INotifyPropertyChanged' (or like behavior), then you can start monitoring for prop changes before AM, then use the list of changed props to set the `Entry..Property..IsNotified` with linq – JoeBrockhaus Jun 25 '15 at 00:20
  • to update values of existing properties , what about just using `context.SaveChanges();` without mentioning `EntityState.Modified;` , like : `existingBlog.Name="testing"; context.SaveChanges();` i mean the method 1 used in [this](http://stackoverflow.com/questions/15336248/entity-framework-5-updating-a-record) question, is that best practice ? – Shaiju T Nov 28 '15 at 09:19