update row in entity frmae work
how to update fields in sql server . i am using entity framework my application.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Pranay RanaPosted Mar 3, 2015, 1:40 AM
1. senario - You are fetching and updating using same context object
In that you can do like this
using (var ctx = new MyDBContext())
var obj = ctx.ObjectLists.FirstOrDefault(o=>o.id==1);
obj.Prop1Value = property value to set;
ctx.SaveChanges();
}
2. Scenario - You are fetching using one context object and updating using another context object
public MyObject GetObject(int id)
{
using (var ctx = new MyDBContext())
{
MyObject obj = ctx.ObjectLists.FirstOrDefault(o=>o.id==1);
}
return obj;
}
public void UpdateObject(Type PropertyValue, MyObject obj)
{
//this line can be out of this method this is just for example
obj.Prop1Value = PropertyValue;
using (var ctx= new MyDBContext())
{
//first attche object
ctx.ObjectList.Attach(obj);
//Second cahnge stat of attached objbect
ctx.ObjectStateManager.ChangeObjectState(obj, System.Data.EntityState.Modified);
//Third save object
ctx.SaveChanges();
}
}
veerendra kumarPosted Mar 3, 2015, 2:14 AM
i am using first scenario.
i got result..
Suraj SahooPosted Mar 3, 2015, 1:27 AM
Please follow the below links, I am sure these will help you.
http://www.entityframeworktutorial.net/EntityFramework4.3/update-entity-using-dbcontext.aspx
http://www.entityframeworktutorial.net/update-entity-in-entity-framework.aspx
Accept if helps anyway.
Thanks