实体框架数据库优先:“时间戳记”列不起作用 [英] Entity Framework DB First: Timestamp column not working

查看:80
本文介绍了实体框架数据库优先:“时间戳记”列不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用db first方法,我希望我的应用程序每次尝试更新一个(过期的)实体时都引发并发异常,该实体已在数据库中的对应行已被另一个应用程序/用户/会话更新。

Using db first approach, I want my application to throw a concurrency exception whenever I try to update an (out-of-date) entity which it's correspoinding row in the database has been already updated by another application/user/session.

我正在.Net 4.5上使用Entity Framework 5。相应的表具有一个Timestamp列来维护行版本。

I am using Entity Framework 5 on .Net 4.5. The corresponding table has a Timestamp column to maintain row version.

推荐答案

我过去通过向其中添加一个timestamp字段来做到这一点。您要执行并发检查的表。 (在我的示例中,我添加了一个名为ConcurrencyCheck的列)

I have done this in the past by adding a timestamp field to the table you wish to perform a concurrency check. (in my example i added a column called ConcurrencyCheck)

根据您的需要,这里有两种类型的并发模式:

There are two types of concurrency mode here depending on your needs :

1并发模式:固定:

然后在模型中重新添加/刷新表。对于固定并发,请确保在将表导入模型时将表的并发模式设置为固定:

Then re-add/refresh your table in your model. For fixed concurrency , make sure your set your concurrency mode to fixed for your table when you import it into your model : like this :

< img src = https://i.stack.imgur.com/2wLle.png alt =在此处输入图片描述>

然后将其捕获:

    try 

    { 

    context.SaveChanges(); 

    } 

    catch (OptimisticConcurrencyException ex) { 


////handle your exception here...

2。并发模式:无

如果您希望处理自己的并发检查,即提出验证通知用户,甚至不允许进行保存,那么您可以将并发模式设置为无。

If you wish to handle your own concurrency checking , i.e. raise a validation informing the user and not even allowing a save to occur then you can set Concurrency mode None.

1。确保在刚添加到的新列的属性中更改 ConcurrencyMode 。无。
2.要在您的代码中使用此代码,我将创建一个变量以在屏幕上存储您当前的时间戳,以检查是否保存。

1.Ensure you change the ConcurrencyMode in the properties of the new column you just added to "None". 2. To use this in your code , i would create a variable to store your current timestamp on the screen you which to check a save on.

private byte[] CurrentRecordTimestamp 
        { 
            get 
            { 
                return (byte[])Session["currentRecordTimestamp"]; 
            } 

            set 
            { 
                Session["currentRecordTimestamp"] = value; 

            } 
        }

1。页面加载(假设您使用的是asp.net,而不是上面没有提到的mvc / razor),或者当您使用希望编辑的数据填充屏幕时,我会将当前记录在Edit的ConcurrencyCheck值下提取到您创建的此变量中。

1.On page load (assuming you're using asp.net and not mvc/razor you dont mention above), or when you populate the screen with the data you wish you edit , i would pull out the current record under edit's ConcurrencyCheck value into this variable you created.

 this.CurrentRecordTimestamp = currentAccount.ConcurrencyCheck;

然后,如果用户将记录保持打开状态,而其他人同时对其进行了更改,则他们也尝试保存,您可以将之前保存的时间戳值与现在的并发值进行比较。

Then if the user leaves the record open , and someone else in the meantime changes it , and then they also attempt to save , you can compare this timestamp value you saved earlier with the concurrency value it is now.

if (Convert.ToBase64String(accountDetails.ConcurrencyCheck) != Convert.ToBase64String(this.CurrentRecordTimestamp)) 
{ 
} 

这篇关于实体框架数据库优先:“时间戳记”列不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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