Click here to Skip to main content
14,209,828 members
Click here to Skip to main content
Alternative
Tip/Trick
Posted 1 Nov 2011

Stats

7.1K views

Extension Method to help with EF Code First updating

,
Rate this:
5.00 (1 vote)
Please Sign up or sign in to vote.
5.00 (1 vote)
1 Nov 2011     CPOL    
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:


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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Daniel Lo Nigro
Web Developer Facebook
United States United States
I'm a web developer specialising in ASP.NET MVC, PHP and JavaScript. I've got several years of web development experience and am always trying new technologies.

Comments and Discussions

 
GeneralReason for my vote of 5 Great stuff, I wasn't aware of the U... Pin
George Danila1-Nov-11 5:13
memberGeorge Danila1-Nov-11 5:13 
GeneralRe: Glad it helped, I only discovered it while reading through t... Pin
Daniel Lo Nigro1-Nov-11 12:33
memberDaniel Lo Nigro1-Nov-11 12:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190617.3 | Last Updated 1 Nov 2011
Article Copyright 2011 by Daniel Lo Nigro
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid