Reputation: 11369
I have a EF entity retrieved and bound to some UI controls using Unit of Work
public RowItem MyItem { get; set; }
using ( var ctx = new MyContext() )
MyItem = ctx.MyEntity.Single( x => /// parms };
at a later time, the updates are saved on some user action
Question - how can i use the existing entity MyItem and keep it simple along the lines of
using ( var ctx = new MyContext() )
MyItem.UpdateUsingContext(ctx);
Upvotes: 0
Views: 104
Reputation: 7961
Well, saving an object in EF is simple:
context.SaveChanges();
This saves all updated/new/deleted object in context
.
If your objects were not created in this particular instance of context
you should attach them before saving:
context.Attach(entity);
context.SaveChange();
Upvotes: 3
Reputation: 14929
If you need to create a new datacontext, you need to first attach your object, something like;
using ( var ctx = new MyContext() )
{
ctx.Attach(entity);
ctx.SaveChanges();
}
If you are resuing the same context, which were used to fetch the entity from database you may simply call savechanges.
Upvotes: 0