显示原始值实体框架7 [英] Show Original Values Entity Framework 7

查看:63
本文介绍了显示原始值实体框架7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个审核表,该表跟踪已添加,已删除和已修改。我在Entity Framework中跟踪此事件,而不是出于多种原因而不是使用数据库触发器,但这确实是因为我们使用流程帐户,并且我想跟踪对记录进行了哪些实际更改的用户。



我曾在EF 5&我不记得我也可以在EF6中使用它。无论哪种方式,我在使用EF 7尝试获取原始值时都遇到了最困难的时刻。



我注意到当我观看手表时-我可以在非公开成员内部看到原始价值-因此在我的脑海中,我知道它必须存在于某个地方。 / p>

最终可以在EF早期版本中使用:

  EntityEntry dbEntry; //这实际上是在另一个区域中传递的,仅作为示例显示。 

foreach(dbEntry.OriginalValues.PropertyNames中的字符串propertyName)
{
//对于更新,我们只想捕获实际改变了
的列,如果(! object.Equals(dbEntry.OriginalValues.GetValue< object>(属性名称),dbEntry.CurrentValues.GetValue< object>(属性名称))))
{
结果。 {
AuditLogID = Guid.NewGuid(),
UserID = userId,
EventDateUTC = changeTime,
EventType = M,//修改后的
TableName = tableName,
RecordID = dbEntry.OriginalValues.GetValue< object>(keyName).ToString(),
ColumnName = propertyName,
OriginalValue = dbEntry.OriginalValues.GetValue< object>(propertyName)==空?null:dbEntry.OriginalValues.GetValue< object>(propertyName).ToString(),
NewValue = dbEntry.CurrentValues.GetValue< object>(prop ertyName)== null吗? null:dbEntry.CurrentValues.GetValue< object>(propertyName).ToString()
}
);
}
}

我得到的错误是EntityEntry不包含对OriginalValues的定义。我要拔头发了...如何使用EF 7从修改后的对象中获取原始值?

解决方案

  //使用System.Reflection; 
foreach(dbEntry.Entity.GetType()。GetTypeInfo()。DeclaredProperties中的var属性)
{
var originalValue = dbEntry.Property(property.Name).OriginalValue;
var currentValue = dbEntry.Property(property.Name).CurrentValue;
Console.WriteLine($ {property.Name}:原始值:{originalValue},当前值:{currentValue});
}


I have an audit table that tracks Added, Deleted and Modified. I track this inside Entity Framework instead of using a Database trigger for multiple reasons but really because we use a Process Account and I want to track what user physically made that change to that record.

I had this working in with EF 5 & I cannot remember I might have had it working in EF6 as well. Either way I am having the hardest time with EF 7 trying to capture the original values.

I noticed that when I am in the watch - I can see Original Values inside the Non-public members - so in my head I know it has to exist somewhere.

Ultimately this works inside EF earlier versions:

EntityEntry dbEntry; //this is actually passed in a different area just showing as an example.

foreach (string propertyName in dbEntry.OriginalValues.PropertyNames)
{
    // For updates, we only want to capture the columns that actually changed
    if (!object.Equals(dbEntry.OriginalValues.GetValue<object>(propertyName), dbEntry.CurrentValues.GetValue<object>(propertyName)))
    {
        result.Add(new TableChange()
        {
            AuditLogID = Guid.NewGuid(),
            UserID = userId,
            EventDateUTC = changeTime,
            EventType = "M",    // Modified
            TableName = tableName,
            RecordID = dbEntry.OriginalValues.GetValue<object>(keyName).ToString(),
            ColumnName = propertyName,
            OriginalValue = dbEntry.OriginalValues.GetValue<object>(propertyName) == null ? null : dbEntry.OriginalValues.GetValue<object>(propertyName).ToString(),
            NewValue = dbEntry.CurrentValues.GetValue<object>(propertyName) == null ? null : dbEntry.CurrentValues.GetValue<object>(propertyName).ToString()
         }
         );
      }
 }

The error I am getting is the EntityEntry does not contain a defition for OriginalValues. I am going to pull my hair out... How do I get the Original Values from a Modified Object with EF 7?

解决方案

// using System.Reflection;
foreach (var property in dbEntry.Entity.GetType().GetTypeInfo().DeclaredProperties)
{
    var originalValue = dbEntry.Property(property.Name).OriginalValue;
    var currentValue = dbEntry.Property(property.Name).CurrentValue;
    Console.WriteLine($"{property.Name}: Original: {originalValue}, Current: {currentValue}");
}

这篇关于显示原始值实体框架7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆