简体   繁体   English

如何首先在Entity Framework代码中关闭开放式并发?

[英]How can I turn off optimistic concurrency in Entity Framework code first?

I am working with an Actian Pervasive (v12) database and Entity Framework 6 and trying to insert a new record. 我正在使用Actian Pervasive(v12)数据库和Entity Framework 6,并尝试插入新记录。 When I do this, I receive a OptimisticConcurrencyException error. 当我这样做时,我收到一个OptimisticConcurrencyException错误。 This behaviour does not happen when I use a MS SQL Server database, even though the tables are constructed the same and both have database generated identity fields. 当我使用MS SQL Server数据库时,即使表的构造相同并且都具有数据库生成的标识字段,这种行为不会发生。

In order to work around this problem I tried changing the [DatabaseGenerated] attribute on the Key field to all the different available values to see if it would behave better and not return the Exception (the Exception is invalid - I am the only user, and I am doing a single insert operation). 为了解决此问题,我尝试将Key字段上的[DatabaseGenerated]属性更改为所有不同的可用值,以查看它的性能是否更好并且不返回Exception(该Exception是无效的-我是唯一的用户,并且我正在执行一次插入操作)。 This didn't help. 这没有帮助。

As a result, I would like to TURN OFF optimistic concurrency in Entity Framework (ie I WANT any updates I do to overwrite whatever is currently in the database). 结果,我想在Entity Framework中关闭乐观并发(即,我想做任何更新来覆盖数据库中当前的内容)。 I do not want Entity Framework to raise this OptimisticConcurrencyException . 我不希望实体框架引发此OptimisticConcurrencyException The problem is, I'm not sure how to do this. 问题是,我不确定如何执行此操作。 My class definition does not currently have a TimeStamp field or any fields with the [ConcurrencyCheck] attribute, and yet it still raises this exception when I perform a simple insert. 我的类定义当前没有TimeStamp字段或具有[ConcurrencyCheck]属性的任何字段,但是当我执行简单的插入操作时,它仍会引发此异常。

Note: My application is developed using DevExpress XAF which is a framework on top of ASP.NET and Entity Framework, so the calls to retrieve data are automatically generated, and it is not easy to override these calls to catch and handle this exception, so I am trying to find a way to prevent Entity Framework from even raising this exception. 注意:我的应用程序是使用DevExpress XAF开发的,DevExpress XAF是ASP.NET和Entity Framework之上的框架,因此检索数据的调用会自动生成,并且不容易覆盖这些调用以捕获和处理此异常,因此我试图找到一种方法来防止实体框架甚至引发此异常。

How can I turn off optimistic concurrency checks in Entity Framework (globally for my application)? 如何关闭Entity Framework中的乐观并发检查(在我的应用程序中全局关闭)?

I found the solution. 我找到了解决方案。

Something I failed to mention in my initial description of the problem is that I am getting this error on classes that have mapped Insert procedures. 在问题的最初描述中,我没有提到的是在映射了插入过程的类上出现此错误。

The exception being raised by EF (OptimisticConcurrency) was somewhat misleading in that the problem was actually that the new ID of the item was not being returned by the stored procedure (Ivan's comment got me thinking about the return value of the sproc). EF(OptimisticConcurrency)引发的异常在某种程度上具有误导性,因为问题实际上是该存储过程未返回该项目的新ID(Ivan的评论使我考虑了该sproc的返回值)。

To fix the problem I had to return the new ID from the stored procedure as follows: 要解决此问题,我必须从存储过程中返回新的ID,如下所示:

SELECT @@IDENTITY AS ID;

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

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