EntityFramework更改跟踪不跟踪更改

时间:2015-09-23 19:14:21

标签: c# entity-framework asp.net-core entity-framework-core

我正在使用Entity Framework 7,我需要能够跟踪更改。我最终在SaveChanges上做了类似的事情我在覆盖它然后在最后使用base.SaveChanges():

foreach (var ent in this.ChangeTracker.Entries().Where(p => p.State == EntityState.Deleted || p.State == EntityState.Modified).ToList())
{
    // For each changed record, get the audit record entries and add them
    foreach (TableChange x in GetTableChangeRecordsForChange(ent, _ChangeUser))
    {
        this.TableChanges.Add(x);
        val = true;
    }
}

这最终要求获取表格更改记录:

private IEnumerable<TableChange> GetTableChangeRecordsForChange(EntityEntry dbEntry, string userId)
{
    List<TableChange> result = new List<TableChange>();    
    foreach (var ent in this.ChangeTracker.Entries().Where(p => p.State == EntityState.Deleted || p.State == EntityState.Modified).ToList())
    {
        // For each changed record, get the audit record entries and add them
        foreach (TableChange x in GetTableChangeRecordsForChange(ent, _ChangeUser))
        {
            this.TableChanges.Add(x);
            val = true;
        }
    }

    if (dbEntry.State == EntityState.Modified)
    {
        foreach (var property in dbEntry.Entity.GetType().GetTypeInfo().DeclaredProperties)
        {

            // For updates, we only want to capture the columns that actually changed
            if (!object.Equals(dbEntry.Property(property.Name).OriginalValue, dbEntry.Property(property.Name).CurrentValue))
            {
                 result.Add(new TableChange()
                 {
                       Action = "U",
                       ColumnName = property.Name,
                       CreatedBy = userId,
                       CreatedOn = DateTime.Now,
                       OldValue = dbEntry.Property(property.Name).OriginalValue.ToString(),
                       NewValue = dbEntry.Property(property.Name).CurrentValue.ToString(),
                       TableName = dbEntry.Entity.GetType().GetTypeInfo().Name,
                       TableID = dbEntry.Property("ID").CurrentValue.ToString()
                       });
              }

          }
    }
}

现在我面临的问题是OriginalValue&amp; CurrentValue和输入的新值。它不跟踪原始值是什么。

我确实:

this.ChangeTracker.AutoDetectChangesEnabled = true;

然而,我仍然无法正确地向我提供原始和当前价值。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

所以我想出了自己的问题......我面临的问题是我附加了需要更新的记录。我最终通过创建更新功能更新了记录:

public T ADMSUpdate<T>(T entity, T originalEntity)
    {
        object propertyValue = null;
        PropertyInfo[] properties = originalEntity.GetType().GetProperties();
        foreach (PropertyInfo property in properties)
        {
            propertyValue = null;
            if (null != property.GetSetMethod())
            {
                PropertyInfo entityProperty = entity.GetType().GetProperty(property.Name);
                propertyValue = entity.GetType().GetProperty(property.Name).GetValue(entity, null);

                if (null != propertyValue)
                {
                    property.SetValue(originalEntity, propertyValue, null);
                }
            }
        }
        return entity;
    }