本文介绍了LINQ to SQL对象版本控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个LINQ to SQL类,该类代表其自身的最新"版本.

I'm trying to create a LINQ to SQL class that represents the "latest" version of itself.

现在,该实体表示的表具有单个自动递增的ID,我当时想在主键上添加一个版本号.我从来没有做过这样的事情,所以我不确定如何进行.我希望能够从使用对象的人那里抽象出对象版本的概念.换句话说,您有一个代表最新版本的实体实例,并且每当提交任何更改时,都会以递增的版本号存储该对象的新副本.

Right now, the table that this entity represents has a single auto-incrementing ID, and I was thinking that I would add a version number to the primary key. I've never done anything like this, so I'm not sure how to proceed. I would like to be able to abstract the idea of the object's version away from whoever is using it. In other words, you have an instance of this entity that represents the most current version, and whenever any changes are submitted, a new copy of the object is stored with an incremented version number.

我应该如何进行呢?

推荐答案

如果可以避免保留历史记录,请这样做.这很痛苦.

If you can avoid keeping a history, do. It's a pain.

如果完整的历史记录(不可避免的财务和医疗数据等)不可避免,请考虑添加历史记录表.使用触发器将版本转换"到历史记录表中.这样,您就不必依赖应用程序来确保记录版本-捕获捕获的所有插入/更新/删除,无论来源如何.

If a complete history is unavoidable (regulated financial and medical data or the like), consider adding history tables. Use a trigger to 'version' into the history tables. That way, you're not dependent on your application to ensure a version is recorded - all inserts/updates/deletes are captured regardless of the source.

如果您的应用需要与历史数据进行交互,请确保它是只读的.如果有人可以简单地更改交易历史记录,则毫无意义.

If your app needs to interact with historical data, make sure it's readonly. There's no sense capturing transaction histories if someone can simply change them.

如果您关注并发更新,请考虑使用记录更改时间戳.当用户A和用户B都在中午查看记录时,他们将获取记录的时间戳.当用户A更新记录时,她的时间戳与记录的时间戳匹配,因此更新将继续进行,并且时间戳也会被更新.用户B在五分钟后更新记录时,其时间戳与记录的时间戳不匹配,因此他警告说自从上次查看记录以来,记录已更改.也许它会自动重新加载...

If your concern is concurrent updates, consider using a record change timestamp. When both User A and User B view a record at noon, they fetch the record's timestamp. When User A updates the record, her timestamp matches the record's so the update goes through and the timestamp is updated as well. When User B updates the record five minutes later, his timestamp doesn't match the record's so he's warned that the record has changed since he last viewed it. Maybe it's automatically reloaded...

无论您做出什么决定,我都会避免将当前数据与历史数据混合在一起.

Whatever you decide, I would avoid inter-mingling current and historic data.

每个评论的触发资源:

  • MSDN
  • A SQL Team Introduction
  • Stackoverflow's Jon Galloway describes a general data-change logging trigger

审核触发器的关键是虚拟表插入"和删除" .这些表包含受INSERT,UPDATE或DELETE影响的行.您可以使用它们来审核更改.像这样:

The keys to an auditing trigger are the virtual tables 'inserted' and 'deleted'. These tables contain the rows effected by an INSERT, UPDATE, or DELETE. You can use them to audit changes. Something like:

CREATE TRIGGER tr_TheTrigger
ON [YourTable]
FOR INSERT, UPDATE, DELETE
AS
    IF EXISTS(SELECT * FROM inserted)
    BEGIN
        --this is an insert or update
        --your actual action will vary but something like this
        INSERT INTO [YourTable_Audit]
            SELECT * FROM inserted
    END
    IF EXISTS(SELECT * FROM deleted)
    BEGIN
        --this is a delete, mark [YourTable_Audit] as required
    END
GO

这篇关于LINQ to SQL对象版本控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 11:06