实施数据库修订的最佳实践是什么?
实施数据库修订的最佳实践是什么?
假设我有一个名为 Test 的表,以及另一个名为 Test_Rev 的修订表。
假设我将跟踪 Test 的记录更改到 Test_Rev 表中。
在保存之前,我获取更改的实体,以便我可以修改需要修改的表。
我使用生产者-消费者模式创建了一个单独的线程,然后将需要修改的实体传递给该线程。
使用访问者模式,我扩展了类的行为,以便我可以修改它们。
它运作良好,但我需要知道最佳实践。
一位朋友告诉我将修订作为我需要修订的表格的触发器。
顺便说一句,我正在使用 VS 2010、SQL Server 2005、实体框架
What is the best practice for implementing a database revisioning?
Assume i have a table called Test, and another table for revisioning called Test_Rev.
It is supposed that i will track Test's records changes into the Test_Rev table.
Before saving, i get the changed entities so that i can revision the tables which needs to be revisioned.
I've made a separate thread using the Producer-Consumer pattern and then pass the entities that i need to revision to that thread.
Using the visitor pattern, i've extended the behavior of my classes so that i can revision them.
It works well but i need to know the best practice.
A friend told me to make the revisioning as a trigger on the tables which i need to revision.
By the way, I'm using VS 2010, SQL Server 2005, Entity Framework
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这取决于您是否对数据库中的逻辑感到满意。如果您没有问题,请使用数据库触发器。如果您想保留应用程序中的逻辑,请覆盖派生
ObjectContext
中的SaveChanges
并在执行base.SaveChanges
之前运行修订(添加修订实体) 。唯一的问题可能是使用自动生成的密钥“审核”新添加的实体 - 仅在调用base.SaveChanges
后才知道该密钥。因此,在这种情况下,您必须在保存更改后审核添加的实体并再次调用base.SaveChanges
。我认为没有任何理由为此拥有单独的线程 - 而且
ObjectContext
不是线程安全的。It depends if you are happy with logic in database. If you don't have problem with it use DB triggers. If you want to keep logic in your application override
SaveChanges
in derivedObjectContext
and run your revisioning (adding revision entities) before you executebase.SaveChanges
. The only problem can be "auditing" newly added entities with autogenerated key - the key is known only after callingbase.SaveChanges
. So in such case you must audit added entities after saving changes and callbase.SaveChanges
again.I don't think there is any reason to have separate thread for that - moreover
ObjectContext
is not thread safe.在数据库中,这称为审核,最常见的是通过触发器完成。在任何情况下都不要在应用程序中执行此操作,否则您将错过更改,执行此操作的唯一合适位置是在数据库中。
In databases this is called auditing and is most commonly done through triggers. Do not under any circumstances do this in the application or you will miss changes, the only appropriate place to do this is in the database.