审计实体框架 [英] Auditing in Entity Framework

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

问题描述

在经历实体框架我有几个问题在实体框架实施审计。

我要存储创建或更新到不同的审计表中每一列的值。

  1. 现在我打电话调用SaveChanges(false)来保存在数据库中(仍在变化背景下不复位)的记录。然后得到加入|通过GetObjectStateEntries修改的记录和循环。但不知道如何获取列的值,他们的值是通过存储过程充满。也就是说,CREATEDATE,modifieddate等。

  2. 下面是示例code,我的工作就可以了。

      //获得改变entires(即记录)
    IEnumerable的< ObjectStateEntry>变化= context.ObjectStateManager.GetObjectStateEntries(EntityState.Modified);
    
    //遍历每个ObjectStateEntry(在更新/修改集合中的每个记录)
    的foreach(在变化ObjectStateEntry项)
    {
        //遍历每个记录的列,并分别获得thier旧的和新的价值
        的foreach(在entry.GetModifiedProperties VAR COLUMNNAME())
        {
            字符串的属性oldValue = entry.OriginalValues​​ [COLUMNNAME]的ToString();
            字符串为newValue = entry.CurrentValues​​ [COLUMNNAME]的ToString();
    
            //发送的entityName,的ColumnName,属性oldValue,newvalue中做一些审计
        }
    }
    
    变化= context.ObjectStateManager.GetObjectStateEntries(EntityState.Added);
    
    的foreach(在变化ObjectStateEntry项)
    {
        如果(entry.IsRelationship)继续;
        VAR columnNames =(由对在entry.EntitySet.ElementType.Members
                           选择p.Name).ToList();
    
        的foreach(在columnNames VAR COLUMNNAME)
        {
            字符串为newValue = entry.CurrentValues​​ [COLUMNNAME]的ToString();
    
            //发送的entityName,的ColumnName,值做一些审计
        }
    }
     

解决方案

在这里,你有两个基本的选择:

  • 请它在数据库级别
  • 请它在C#code

做它在数据库层面,是指使用触发器。在这种情况下存在,如果您使用的是企业库或其他数据访问技术是没有区别的。

要做到这一点,在C#code,你会添加一个日志表到你的数据模型,并写入更改日志表。当你做了更改保存两个更改的数据和你写给将要保存的日志表中的信息。

After going through Entity Framework I have a couple of questions on implementing auditing in Entity Framework.

I want to store each column values that is created or updated to a different audit table.

  1. Right now I am calling SaveChanges(false) to save the records in the DB(still the changes in context is not reset). Then get the added | modified records and loop through the GetObjectStateEntries. But don't know how to get the values of the columns where their values are filled by stored proc. ie, createdate, modifieddate etc.

  2. Below is the sample code I am working on it.

    // Get the changed entires( ie, records)
    IEnumerable<ObjectStateEntry> changes = context.ObjectStateManager.GetObjectStateEntries(EntityState.Modified);
    
    // Iterate each ObjectStateEntry( for each record in the update/modified collection)
    foreach (ObjectStateEntry entry in changes)
    {
        // Iterate the columns in each record and get thier old and new value respectively
        foreach (var columnName in entry.GetModifiedProperties())
        {
            string oldValue = entry.OriginalValues[columnName].ToString();
            string newValue = entry.CurrentValues[columnName].ToString();
    
            // Do Some Auditing by sending entityname, columnname, oldvalue, newvalue
        }
    }
    
    changes = context.ObjectStateManager.GetObjectStateEntries(EntityState.Added);
    
    foreach (ObjectStateEntry entry in changes)
    {
        if (entry.IsRelationship) continue;
        var columnNames = (from p in entry.EntitySet.ElementType.Members
                           select p.Name).ToList();
    
        foreach (var columnName in columnNames)
        {
            string newValue = entry.CurrentValues[columnName].ToString();
    
            // Do Some Auditing by sending entityname, columnname, value
        }
    }
    

解决方案

Here you have two basic options:

  • Do it at the database level
  • Do it in the c# code

Doing it at the data base level, means using triggers. In that case there is no difference if you are using enterprise library or another data access technology.

To do it in the C# code you would add a log table to your datamodel, and write the changes to the log table. When you do a save changes both the changes to the data and the information which you wrote to the log table would be saved.

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

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