简体   繁体   English

实体框架更改跟踪API和参考条目

[英]Entity Framework Change Tracking API and reference entries

Looking to write generic Audit code on my DbContext subclass. 希望在我的DbContext子类上编写通用审计代码。

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) If I only change a reference property, the entry.State value is "Unchanged". 1)如果我只更改引用属性,则entry.State值为“Unchanged”。

2) Even if point 1 was set to "Modified", the DbReferenceEntry class doesn't seem to have an IsModified property, nor an original value. 2)即使将第1点设置为“已修改”,DbReferenceEntry类似乎也没有IsModified属性,也没有原始值。

I assume this is possible because EF must be tracking this. 我认为这是可能的,因为EF必须跟踪这一点。

Can anyone help? 有人可以帮忙吗?

Thanks, Ben 谢谢,本

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. 在ObjectContext API中,您可以通过ObjectStateManager获取这些对象,但看起来DbContext API没有这个可用。 I asked a question about this on MSDN Forum . 我在MSDN论坛上问过这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM