4

使用 ObjectContext:

var objContext = new ObjContextEntities();
var accountType = objContext.AccountTypes.FirstOrDefault(x => x.Id == 0);
accountType.Name = "ABC";
var stateEntry = objContext.ObjectStateManager.GetObjectStateEntry(accountType);
Console.WriteLine(stateEntry.GetModifiedProperties().Count());  //--------> Outputs 1 as expected

使用 DbContext:

var dbContext = new DbContextEntities();
var accountType = dbContext.DBAccountTypes.FirstOrDefault(x => x.Id == 0);
accountType.Name = "XYZ";
var dbObjContext = ((IObjectContextAdapter)dbContext).ObjectContext;
var stateEntry = dbObjContext.ObjectStateManager.GetObjectStateEntry(accountType);
Console.WriteLine(stateEntry.GetModifiedProperties().Count());    //--------> Outputs 0

我想迁移到使用 DbContext,但我有依赖此功能的代码。这是一个已知的错误?任何人都可以提出另一种方法吗?谢谢。

4

1 回答 1

8

好的,这似乎可以解决问题:

var dbContext = new DbContextEntities();
var accountType = dbContext.DBAccountTypes.FirstOrDefault(x => x.Id == 0);
accountType.Name = "XYZ";
var entry = dbContext.Entry(accountType);
var modifiedProperties = entry.CurrentValues.PropertyNames.Where(propertyName => entry.Property(propertyName).IsModified).ToList();
Console.WriteLine(modifiedProperties.Count());    //--------> Outputs 1

更多有用的信息在这里:http: //msdn.microsoft.com/en-US/data/jj592677

于 2013-03-14T14:55:53.487 回答