gpt4 book ai didi

entity-framework - GetModifiedProperties 不适用于 EF4.1 DbContext

转载 作者:行者123 更新时间:2023-12-04 23:24:25 25 4
gpt4 key购买 nike

使用 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,但我有依赖此功能的代码。这是一个已知的错误?任何人都可以建议另一种方法吗?谢谢。

最佳答案

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

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

关于entity-framework - GetModifiedProperties 不适用于 EF4.1 DbContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15356674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com