使用实体框架创建审计跟踪的更好方法

时间:2017-12-01 11:06:16

标签: entity-framework audit change-tracking

我已经查看了许多使用Entity Framework创建审计跟踪的示例,但尚未找到适合我的任何内容。通过简单地覆盖数据库上下文中的SaveChanges并使用ChangeTracker,我必须有一个灵活/简洁的方法来执行它...我遇到的问题是诸如添加(创建)实体之前它没有ID的事情之后你保存它,当你保存它时,它似乎爆炸了变化跟踪器中的内容。无论如何,我有一个审计线索工作,但它很难看,我正在寻求帮助,简化这一点,使我不必添加到一个可怕的if-then每次我添加一个实体!任何和所有帮助表示赞赏。

    public bool CreateRecord(object o)
    {
        Audit audit = new Audit()
        {
            ChangeTypeID = (int)Audit.ChangeType.CREATE,
            TimeStamp = GetCurrentDateTime(),
            RecordClass = o.GetType().ToString(),
            NewValue = "",
            ReasonForChange = "Record Creation"
        };

        if (o.GetType() == typeof(Permission))
        {
            Permission x = (Permission)o;
            audit.OriginalValue = x.ToString();
            Permissions.Add(x);
            SaveChanges();
            audit.RecordID = x.ID;
        }
        else if (o.GetType() == typeof(User))
        {
            User x = (User)o;
            audit.OriginalValue = x.ToString();
            Users.Add(x);
            SaveChanges();
            audit.RecordID = x.ID;
        }
        else if (o.GetType() == typeof(LogIn))
        {
            LogIn x = (LogIn)o;
            audit.OriginalValue = x.ToString();
            LogIns.Add(x);
            SaveChanges();
            audit.RecordID = x.ID;
        }
        else if (o.GetType() == typeof(Marker))
        {
            Marker x = (Marker)o;
            audit.OriginalValue = x.ToString();
            Markers.Add(x);
            SaveChanges();
            audit.RecordID = x.ID;
        }
        else if (o.GetType() == typeof(Method))
        {
            Method x = (Method)o;
            audit.OriginalValue = x.ToString();
            Methods.Add(x);
            SaveChanges();
            audit.RecordID = x.ID;
        }
        else if (o.GetType() == typeof(Sample))
        {
            Sample x = (Sample)o;
            audit.OriginalValue = x.ToString();
            Samples.Add(x);
            SaveChanges();
            audit.RecordID = x.ID;
        }
        else if (o.GetType() == typeof(Run))
        {
            Run x = (Run)o;
            audit.OriginalValue = x.ToString();
            Runs.Add(x);
            SaveChanges();
            audit.RecordID = x.ID;
        }
        else if (o.GetType() == typeof(XYDataSet))
        {
            XYDataSet x = (XYDataSet)o;
            audit.OriginalValue = x.ToString();
            XYDataSets.Add(x);
            SaveChanges();
            audit.RecordID = x.ID;
        }
        else
        {
            return false;
        }

        // Save audit record
        audit.UserID = ((App)Application.Current).GetCurrentUserID();
        Audits.Add(audit);
        SaveChanges();

        return true;
    }

1 个答案:

答案 0 :(得分:0)

我假设所有实体都属于同一个项目/程序集,所以你可以尝试类似的东西,注意这个代码没有经过测试,可能需要修改。

    public bool CreateRecord(object o)
    {
        Audit audit = new Audit()
        {
            ChangeTypeID = (int)Audit.ChangeType.CREATE,
            TimeStamp = GetCurrentDateTime(),
            RecordClass = o.GetType().ToString(),
            NewValue = "",
            ReasonForChange = "Record Creation"
        };
        var entityType = Assembly.GetAssembly(typeof(Permission)).GetTypes().Where(x => x == o.GetType())
            .FirstOrDefault(); // Determine the desired entity (assumed all of the entities belongs to same project/assembly)
        if (entityType != null)
        {
            var convertedObject = Convert.ChangeType(o, entityType); // Convert object to entity
            audit.OriginalValue = convertedObject.ToString();
            var entity = yourContext.Set(entityType).Add(convertedObject); // Get DbSet for casted entity
            SaveChanges();
            audit.RecordID = x.ID;
        }
        else
        {
            return false;
        }

        // Save audit record
        audit.UserID = ((App)Application.Current).GetCurrentUserID();
        Audits.Add(audit);
        SaveChanges();

        return true;
    }