Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF entity.attach does not work

I have an entity retrieved from db as follows

using ( var ctx = new Mycontext() )
   return ctx.MyGroups.First( // query );

this is bound to UI and updated on user save action as follows

using ( var ctx = new Mycontext() )
{
    ctx.MyGroups.Attach(o); // verified object o is updated 
    ctx.SaveChanges();

}

However the db is NOT updated

Environment is .net 4.0, db is sql compact 4

Any help on what could be missing/wrong ?

like image 708
Kumar Avatar asked Dec 21 '22 22:12

Kumar


1 Answers

When you attach objects to the context, their default state is Unchanged, you should force the update by setting DBContext.Entry(entity).State = EntityState.Modified; And only after that call DBContext.SaveChanges();

like image 174
Ciobanu Ion Avatar answered Jan 11 '23 22:01

Ciobanu Ion