Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Extension Method to help with EF Code First updating

0.00/5 (No votes)
1 Nov 2011 1  
As far as I know, the UpdateModel method is designed to do this. Something like this should work, but I haven't tested it:public ActionResult Edit(Entity entity){ var entityInDbSet = _context.Set.SingleOrDefault(x => x.Id == entity.Id); UpdateModel(entityInDbSet); ...

As far as I know, the "UpdateModel" method is designed to do this. Something like this should work, but I haven't tested it:


C#
public ActionResult Edit(Entity entity)
{
    var entityInDbSet = _context.Set.SingleOrDefault(x => x.Id == entity.Id);
    UpdateModel(entityInDbSet);
    _context.SaveChanges();
}

There's also an overload that lets you specify a list of the properties to update. This is recommended for security reasons, so users can't update properties they shouldn't be allowed to update. See http://msdn.microsoft.com/en-us/library/dd492193.aspx[^] for more information. :)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here