有什么好方法来确定当一个实体的数据被更改的数据库呢? [英] What is a good method to determine when an entities data has been changed on the database?

查看:166
本文介绍了有什么好方法来确定当一个实体的数据被更改的数据库呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说,利用实体框架,我检索的实体从数据库中。有没有什么办法随后检查相同的特定实体已经在数据库中其他用户更改?

Say, using Entity Framework, I have retrieved an Entity from the database. Is there any way to check subsequently that the same specific Entity has changed in the database by another user?

我已经使用了previously(在WinForm的应用程序)的方法是:

The method I have used previously (in WinForm applications) was to:

  • 保存实体到数据库
  • 在一个条目添加到一个事务表与实体类型,唯一标识符和日期时间改
  • 当刷新同一实体,检查到当前用户的保存后续行的交易表中。
  • 如果找到一个入口,采取相应的行动(从数据库中,块编辑重装实体,保存的冲突等视为什么我检查的变化)。

我想这样做的原因是:

  • 避免重装一整套实体,只是刷新已更改的实体
  • 检查上保存并发冲突
  • 基于编辑的时候就开始解决并发冲突。要做到这一点,我需要做的事务表中的条目,当用户的开始的编辑,当用户保存更新同一事务表行/取消。
  • Avoid reloading an entire set of Entities and just refresh the Entities that have changed
  • Check for concurrency conflicts on Save
  • Resolve concurrency conflicts based when Editing began. To do this I need to make an entry in the Transactions table when a user begins editing and update the same Transaction table row when the user saves / cancels.

请问这个方法似乎对不对?是这样做的原因,即使正确的呢?也许有已经功能来做到这一点,我一直没能找到?

Does this method seem right? Are the reasons for doing this even correct at all? And perhaps there's already functionality to do this that I haven't been able to find?

推荐答案

我不知道你为什么不使用开放式并发的更标准的方式之一。英孚已经支持这一点。我认为,一个单独的事务表只增加了复杂性,因为你总是必须明确地保持在该表中的条目,而这样做的CRUD操作上的主要实体。当用户开始编辑并创建了交易表中的锁定项,但随后崩溃,会发生什么?会不会对数据库中的记录的排它锁是适当的位置(即悲观的锁定)。

I wonder why you don't use one of the more standard ways of optimistic concurrency. EF has support for this. I think a separate Transactions table only adds complexity because you always have to explicitly maintain entries in that table while doing CRUD operations on the main entities. What happens when a user begins editing and creates a "locking" entry in the Transactions table, but then crashes? Wouldn't an exclusive lock on the record in the database be appropriate here (i.e. pessimistic locking).

您也可以用这种方法来检查中间的变化,因为你可以执行的时间戳或版本列轻查询。

You can also use this approach to check for intermediate changes, because you can execute a light query on the timestamp or version column.

这篇关于有什么好方法来确定当一个实体的数据被更改的数据库呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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