How to update model in the database, from asp.net MVC2, using Entity Framework? -
i'm building asp.net mvc2 application, , using entity framework orm. having troubles updating object in database. every time try entity.savechanges(), ef inserts new line in table, regardless of want update, or insert done. tried attaching (like in next example) object entity, got
{"an object null entitykey value cannot attached object context."}
here's simple function inserts , updates (it's not vehicles, it's simpler explain this, although don't think effects answers @ all)...
public static void insertorupdatecar(this vehicles entity, cars car) { if (car.id == 0 || car.id == null) { entity.cars.addobject(car); } else { entity.attach(car); } entitet.savechanges(); }
i tried using attachto("cars", car), got same exception.
anyone has experience this?
if updating existing record should have entitykey within object instance providing insertorupdate method. go code , see if can locate getting lost. i'd suspect presenting user form update object , mapping response fields car object aren't passing entitykey (you don't want display user).
what you'd need include key in form using input type of 'hidden'. can use html helper html.hidden("key field name", key field value) ensure it's passed user form , post code.
Comments
Post a Comment