How to use Entity Framework to change an object

ado.net entity-framework

Question

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

1
2
8/7/2010 3:47:08 PM

Accepted Answer

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();
5
3/7/2012 3:41:18 PM

Popular Answer

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();
        }


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow