Correct way to perform an update using ADO.Net Entity Model in .net 4

Posted by sf on Stack Overflow See other posts from Stack Overflow or by sf
Published on 2010-05-13T17:28:29Z Indexed on 2010/05/15 22:10 UTC
Read the original article Hit count: 504

Hi,

I just wanted to clarify if this is the appropriate way to perform an update using a designer generated ADO.NET Entity Model.

I was wondering if there was a way to perform an update without creating the sqlMenu object.

public class MenusRepository
{
    public void UpdateMenu(Menu menu)
    {
        // _context is instantiated in constructor
        Menu sqlMenu = (from m in _context.Menus where m.MenuId == menu.MenuId select m).FirstOrDefault();

        if (sqlMenu == null)
            throw new ArgumentException("Can't update a Menu which does not exist");

        // associate values here
        sqlMenu.Name = menu.Name;

        _context.SaveChanges();
    }
}

© Stack Overflow or respective owner

Related posts about .net-4.0

Related posts about ADO.NET