How to use Entity Framework to change an object

ado.net entity-framework

Question

Although I can add data, I'm not sure how to change the data. I'm receiving the AddObject and DeleteObject methods, but I can't find any update methods.

Thanks

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

Accepted Answer

Simply take one or more objects, move them around, and dialSaveChanges on the surroundings. Of course, tracking has to be enabled and the object needs to be tied to the context.

var obj = context.table.First(o => o.ID == 1);
obj.Property1 = data;
context.SaveChanges();
5
3/7/2012 3:41:18 PM

Popular Answer

You may think of the following code fragment, which is taken from Worker Information Starter Kit:

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