ADO.NET Entity Framework : Editing a detached Object
In Layered scenario, you might need to pass an object to another method while updating. Carrying open Context is not good however you may try the below approach by using “CreateEntityKey”. But this will not work if you have configured your edmx with Stored Procedure, reason behind that is
static void Main(string[] args)
{
//Create Entity Key Object
EntityKey key;
object originalItem;
//This object can come through method parameter
Emp updatedItem = new Emp() { Id = 190 };
updatedItem.FirstName = "Changed Last Name";
using (TestDBEntities ctx = new TestDBEntities())
{
key = ctx.CreateEntityKey("Emp", updatedItem);
if(ctx.TryGetObjectByKey(key, out originalItem))
{
ctx.ApplyPropertyChanges(key.EntitySetName, updatedItem);
}
ctx.SaveChanges();
}
}
Namoskar!!!