ChangeTracker.Entries()CurrentValue等于EF7中的OriginalValue

时间:2016-05-13 13:08:14

标签: c# entity-framework entity-framework-core

我在EF7 / asp.Net Core应用程序中遇到了问题。在我的上下文中,我创建了一个方法Save:

2016-05-13 13:53:38 [INFO ] 0 repositories identified with calculated folder sizes in 5 msecs

和方法GetPrimaryKeyValue:

    public int Save()
    {
        ChangeTracker.DetectChanges();
        var modifiedEntities = ChangeTracker.Entries()
            .Where(p => p.State == EntityState.Modified || p.State == EntityState.Added || p.State == EntityState.Deleted || p.State == EntityState.Modified || p.State == EntityState.Detached).ToList();
        var now = DateTime.UtcNow;

        foreach (var change in modifiedEntities)
        {
            var entityName = change.Entity.GetType().Name;
            var primaryKeyValue = GetPrimaryKeyValue(change.Entity);
            foreach (var prop in change.Entity.GetType().GetTypeInfo().DeclaredProperties)
            {
                if (!prop.GetGetMethod().IsVirtual)
                {
                    var currentValue = change.Property(prop.Name).CurrentValue;
                    var originalValue = change.Property(prop.Name).OriginalValue;
                    if (originalValue.ToString() != currentValue.ToString())
                    {
                        var changeLoged = new ChangeLog
                        {
                            PropertyName = prop.Name,
                            EntityName = entityName,
                            PrimaryKeyValue = primaryKeyValue,
                            DateChange = now,
                            OldValue = originalValue.ToString(),
                            NewValue = currentValue.ToString(),
                            ChangedBy = "test"
                        };
                        ChangeLog.Add(changeLoged);
                    }
                }
            }
        }
        return base.SaveChanges();
    }

不幸的是,change.Property(prop.Name).CurrentValue总是等于OriginalValue,所以if protected virtual int GetPrimaryKeyValue<T>(T entity) { var test = entity; var test2 = test.GetType(); var keyName = this.Model.FindEntityType(test2).FindPrimaryKey().Properties .Select(x => x.Name).Single(); var result = (int)entity.GetType().GetProperty(keyName).GetValue(entity, null); if (result < 0) return -1; return result; }

总是返回false。

2 个答案:

答案 0 :(得分:4)

这不能完全回答您的问题,因为我无法重现此问题,但这可能会对您有所帮助。

在EF Core中, PropertyEntry 类现在有一个 IsModified 属性,可以让您知道该值是否已被修改。

您应该使用它:

if (change.Property(prop.Name).IsModified)
{
    var changeLoged = new ChangeLog
    {
        PropertyName = prop.Name,
        EntityName = entityName,
        PrimaryKeyValue = primaryKeyValue,
        DateChange = now,
        OldValue = originalValue.ToString(),
        NewValue = currentValue.ToString(),
        ChangedBy = "test"
    };
    ChangeLog.Add(changeLoged);
}

免责声明:我是该项目的所有者Entity Framework Plus

图书馆有一个审核功能(支持EF Core),您可以使用它或获得灵感来创建审核(代码是开源的)。

文档:EF+ Audit

答案 1 :(得分:1)

替换:

var originalValue = change.Property(prop.Name).OriginalValue;

收件人:

var originalValue = change.GetDatabaseValues().GetValue<object>(prop.Name);