简体   繁体   English

基于实体框架推送的更改跟踪

[英]Entity Framework push-based change tracking

Does Entity Framework innately support any sort of change tracking in terms of detecting which records in a database were added after some date x and which were added before some date x?实体框架是否天生就支持任何类型的更改跟踪,以检测数据库中的哪些记录是在某个日期 x 之后添加的,哪些是在某个日期 x 之前添加的? I know it supports tracking changes in the properties of entities themselves, but this is a bit different, I think.我知道它支持跟踪实体本身属性的变化,但我认为这有点不同。

If not in Entity Framework, I'm wondering what would be recommended for a simple way to achieve this.如果不在实体框架中,我想知道推荐什么简单的方法来实现这一点。

The scenario would be that automatic push-based data transfers are occurring say, every hour, and each time a push is initiated the server has to know which records have been added since the last push, and push those.这种情况是自动基于推送的数据传输发生,比如说,每小时,每次发起推送时,服务器必须知道自上次推送以来添加了哪些记录,并推送这些记录。

Does anyone know a standard and reliable approach to this?有谁知道一个标准和可靠的方法? I've created my own solutions (watching primary keys, DateTime columns, etc.) but would rather not reinvent the wheel.我创建了自己的解决方案(查看主键、DateTime 列等),但不想重新发明轮子。

The feature you're looking for are exactly Change Tracking (tracks the rows changed for sync like scenarios) and/or Change Data Capture (tracks exactly what changes, including pre-change image of data, for more complex scenarios like audit).您正在寻找的功能正是更改跟踪(跟踪为同步类似场景而更改的行)和/或更改数据捕获(准确跟踪哪些更改,包括数据的更改前图像,以用于更复杂的场景,如审计)。 There is a comparison of the two at Comparing Change Data Capture and Change Tracking , which covers also the main scenarios each of these two addresses.比较变更数据捕获和变更跟踪中对两者进行了比较,其中还涵盖了这两个地址的主要场景。 Note that 'roll your own' does not work because you need special support in the engine to either detect certain kind of changes, or prevent operations that would result in breaking the change tracking.请注意,“自己动手”不起作用,因为您需要引擎中的特殊支持来检测某些类型的更改,或防止可能导致中断更改跟踪的操作。

Entity Framework has nothing to do with these operations. Entity Framework 与这些操作无关。

I don't think Entity Framework provides what you are looking for.我不认为实体框架提供你正在寻找的东西。 I could think of two solutions out of the top of my head (Assuming you're using MS SQL Server):我可以想到两种解决方案(假设您使用的是 MS SQL 服务器):

Use a rowversion column and have your update check routing update another column with the current rowversion.使用rowversion列并让您的更新检查路由使用当前 rowversion 更新另一列。 If, when the change checking routing runs again, the rowversion does not match the rowversioncopy you know it has been modified.如果,当更改检查路由再次运行时,rowversion 与您知道它已被修改的 rowversioncopy 不匹配。 This may or may not be a feasible approach depending on how many rows you're dealing with.这可能是也可能不是一种可行的方法,具体取决于您要处理的行数。

Another approach would be to use triggers on tables and have them fire your change handling.另一种方法是在表上使用触发器并让它们触发您的更改处理。

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

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