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



我浏览了许多使用实体框架创建审计跟踪的示例,但尚未找到对我有用的任何东西。只需在DB上下文中简单地覆盖Savechanges并使用Changetracker ...我遇到的问题是添加(创建)实体,直到之后才有ID,就必须有一种光滑/简短的方法来完成此操作。您可以保存它,并保存它时,它似乎爆炸了变更跟踪器中的内容。无论如何,我有一条审核的步道工作,但这很丑陋,我一直在寻求帮助,以简化这一点,并使其使我不必每次添加一个实体时都不必添加到可怕的情况下!所有的帮助都赞赏。

    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;
    }

我假设所有实体都属于同一项目/汇编,因此您可以尝试类似的东西,并注意到该代码未测试,可能需要修改。P>

    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;
    }

最新更新