实体框架更改跟踪API和参考条目 [英] Entity Framework Change Tracking API and reference entries
问题描述
展望编写通用的审计code在我的DbContext子类。
Looking to write generic Audit code on my DbContext subclass.
foreach (var entry in this.ChangeTracker.Entries<MyClass>())
{
if (entry.State == EntityState.Modified)
{
var entityProperties = entry.Entity.GetType().GetProperties();
foreach (var entityProperty in entityProperties)
{
DbMemberEntry propertyEntry = entry.Member(property.Name);
if (propertyEntry is DbPropertyEntry)
{
// IsModified available
}
else if (propertyEntry is DbReferenceEntry)
{
// IsModified not available
}
}
}
}
1)如果我只改变一个参考性的entry.State值是不变。
1) If I only change a reference property, the entry.State value is "Unchanged".
2),即使点1被设置为修改,则DbReferenceEntry类似乎不具有通过isModified属性,也不是原始值。
2) Even if point 1 was set to "Modified", the DbReferenceEntry class doesn't seem to have an IsModified property, nor an original value.
我想这是可能的,因为EF必须跟踪这一点。
I assume this is possible because EF must be tracking this.
谁能帮助?
谢谢, 本
推荐答案
是参考条目(导航性能)不跟踪更改。这是外键属性(如遇外键关联的)或独立协会单独的对象跟踪变化的责任。在ObjectContext的API,你可以得到这些对象通过 ObjectStateManager
,但它看起来像的DbContext API没有这个用。我问这个的<一个问题href="http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/192f6f64-df77-4902-901b-c36e2b157ab6">MSDN论坛。
Yes reference entry (navigation property) does not track changes. It is responsibility of foreign key property (in case of foreign key association) or separate object tracking changes of independent association. In ObjectContext API you can get these objects by ObjectStateManager
but it looks like DbContext API doesn't have this available. I asked a question about this on MSDN Forum.
这篇关于实体框架更改跟踪API和参考条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!