开发者

How to update an object using Entity Framework

I am able to add data开发者_高级运维, but not sure how should I update the data. I am getting AddObject,DeleteObject methods not found any method to update.

Thanks


You simply grab an (or multiple) object(s), manipulate them and call SaveChanges on the context. Of course, the object has to be attached to the context and tracking must enabled.

var obj = context.table.First(o => o.ID == 1);
obj.Property1 = data;
context.SaveChanges();


Taken from Employee Info Starter Kit, you can consider the code snippet as below:

public void UpdateEmployee(Employee updatedEmployee)
        {
            //attaching and making ready for parsistance
            if (updatedEmployee.EntityState == EntityState.Detached)
                _DatabaseContext.Employees.Attach(updatedEmployee);
            _DatabaseContext.ObjectStateManager.ChangeObjectState(updatedEmployee, System.Data.EntityState.Modified);
            _DatabaseContext.SaveChanges();
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜