开发者

How to update an object using Entity Framework

开发者 https://www.devze.com 2023-01-10 19:59 出处:网络
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.

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

精彩评论

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

关注公众号