ObjectStateManager没有定义问题

时间:2015-09-18 13:18:59

标签: c# asp.net-mvc entity-framework

我遇到ObjectStateManager的问题。

我是using System.Data.EntitySystem.Data.Objects,但它没有显示ObjectStateManager的定义。

SampleContext db = new SampleContext();
Employees employeeFromDB = db.Employees.Single(x => x.Id == employee.Id);

employeeFromDB.FullName = employee.FullName;
employeeFromDB.Gender = employee.Gender;
employeeFromDB.Age = employee.Age;
employeeFromDB.HireDate = employee.HireDate;
employeeFromDB.Salary = employee.Salary;
employeeFromDB.PersonalWebSite = employee.PersonalWebSite;

db.ObjectStateManager.ChangeObjectState(employeeFromDB, System.Data.EntityState.Modified);
db.SaveChanges();
return RedirectToAction("Details", new {Id = employee.Id});

2 个答案:

答案 0 :(得分:0)

来自:Why does the ObjectStateManager property not exist in my db context?

var manager = ((IObjectContextAdapter)db).ObjectContext.ObjectStateManager;

答案 1 :(得分:0)

使用此代码:

db.Entry(employeeFromDB).State = EntityState.Modified;
//db.ObjectStateManager.ChangeObjectState(employeeFromDB,System.Data.EntityState.Modified);
db.SaveChanges();