简体   繁体   English

变更跟踪实体框架

[英]Change Tracking Entity framework

i make table for property name that changed and value before and value after我为更改的属性名称和值之前和之后的值制作表格
How i can use Change Tracking to store changed in this table ?我如何使用更改跟踪在此表中存储更改?

You can track the operation, the changed columns and the new values by using Change Tracking.您可以使用更改跟踪来跟踪操作、更改的列和新值。 However getting the old Value out of Change Tracking is not possible.然而,从变更跟踪中获取旧值是不可能的。 SQL Server 2016 offers the new feature "Change data capture", which gives you the needed Information about the old value before the update/delete happened ( see https://msdn.microsoft.com/en-us/library/bb933994.aspx ). SQL Server 2016 提供了新功能“更改数据捕获”,它在更新/删除发生之前为您提供有关旧值的所需信息(请参阅https://msdn.microsoft.com/en-us/library/bb933994.aspx )。

If you don't have access to a SQL Server 2016, here is how you can configure Change Tracking:如果您无权访问 SQL Server 2016,您可以通过以下方式配置更改跟踪:

  • Activate at Database在数据库激活

    ALTER DATABASE <YourDatabase> fe DeviceDatabase SET CHANGE_TRACKING = ON (CHANGE_RETENTION = 2 DAYS, AUTO_CLEANUP = ON)
  • Activate Change Tracking for your needed tables为您需要的表激活更改跟踪

    ALTER TABLE <YourTable> fe Devices ENABLE CHANGE_TRACKING WITH (TRACK_COLUMNS_UPDATED = ON)
  • Setup a DB Job, which will copy change-information into your custom table every minute,hour,day (what you need)设置一个数据库作业,它将每分钟、每小时、每天(您需要什么)将更改信息复制到您的自定义表中

    DECLARE @minversion bigint; SET @minversion = (SELECT MinVersion = CHANGE_TRACKING_MIN_VALID_VERSION(OBJECT_ID('Devices')) ) SELECT SYS_CHANGE_COLUMNS, e.Id FROM CHANGETABLE(CHANGES Devices, @minversion) AS C LEFT OUTER JOIN Devices AS e ON e.Id = c.Id;
  • To Get the latest Value of the Changed Column you can try this (but beware of multiple updates of the same row. you only get the latest value).要获取更改列的最新值,您可以尝试此操作(但要注意同一行的多次更新。您只能获得最新值)。

     CHANGE_TRACKING_IS_COLUMN_IN_MASK (COLUMNPROPERTY(OBJECT_ID('Devices'), 'Id', 'ColumnId') ,c.sys_change_columns)

This will return 1 if Column changed, 0 if not.如果列更改,则返回 1,否则返回 0。 You can add this for every column of your table and join on value = 1 and then add the value to your query.您可以为表的每一列添加它并在 value = 1 上加入,然后将该值添加到您的查询中。

Finally, I would just recommend to use Stored Procedures to Update/Insert/Delete on your Tables .最后,我只建议使用存储过程来更新/插入/删除您的表 In those you can easily insert all information you want to store about the change in your custom table.在那些中,您可以轻松插入要存储的有关自定义表中更改的所有信息。 If you have SQL Server 2016 tho, try what I mentioned above, eventually.如果您有 SQL Server 2016,请最终尝试我上面提到的内容。

Actually if you override the SaveChanges() method in your data context class you can access ChangeTracker .实际上,如果您覆盖数据上下文类中的SaveChanges()方法,则可以访问ChangeTracker This gives you all the entities currently tracked by the context and their EntityState (if they are added/modified/deleted/unchanged etc).这为您提供了上下文当前跟踪的所有实体及其EntityState (如果它们被添加/修改/删除/未更改等)。

Here you can get the DbEntityEntry class and from that get the entitys current values and/or its previous values if the entity is in the modified state.如果实体处于修改状态,您可以在这里获取DbEntityEntry类,并从中获取实体的当前值和/或其以前的值。

public override int SaveChanges()
{
    var allTrackedEntities = this.ChangeTracker.Entries().ToList();

    return base.SaveChanges();
}

I currently use this method to do some basic auditing of who is doing that to what entity.我目前使用这种方法对谁在对什么实体做一些基本的审计。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM