简体   繁体   English

实体框架中使用存储过程的乐观并发

[英]Optimistic Concurrency using Stored Procedures in Entity Framework

In the entity framework, using stored procedures, I would like to perform an update on my table using optimistic concurrency. 在实体框架中,使用存储过程,我想使用乐观并发在我的表上执行更新。 I've been unable to get this working, without modifying my existing update stored procedure. 在没有修改现有更新存储过程的情况下,我一直无法使用它。 I'm trying to determine if there is a way I can map my existing stored procedure so that a concurrency exception will occur when no rows are updated. 我正在尝试确定是否有一种方法可以映射现有的存储过程,以便在没有更新行时发生并发异常。

Some background information: 一些背景资料:

  • I've mapped the update stored procedure (including the timestamp) column in my .edmx file 我已经在我的.edmx文件中映射了更新存储过程(包括时间戳)列
  • My existing stored procedure looks like the following (the actual table names and columns are obviously omitted): 我现有的存储过程如下所示(显然省略了实际的表名和列):

     UPDATE Table SET Column = @Column1, Column2 = @Column2 .... WHERE PK = @PK AND Timestamp = @Timestamp SELECT PK, Column1, Column2, ....., Timestamp FROM Table WHERE PK = @PK 
  • In the event that the update fails (due to a timestamp mismatch) the select portion of the stored procedure will still return a row. 在该更新失败(由于时间戳不匹配)的情况下的存储过程的选择部分仍然返回一行。

When I modify the stored procedure to the following: 当我将存储过程修改为以下内容时:

   UPDATE Table SET Column = @Column1, Column2 = @Column2 ....
   WHERE PK = @PK AND Timestamp = @Timestamp

   IF @@ROWCOUNT > 0
       SELECT PK, Column1, Column2, ....., Timestamp
       FROM Table WHERE PK = @PK

Then everything works as expected, and the concurrency error occurs. 然后一切都按预期工作,并发生并发错误。

Alternatively if I make the stored procedure return an output parameter, and map that output parameter to the "Rows Affected Parameter" in the Entity Framework's .edmx file, then the concurrency error also works as expected. 或者,如果我使存储过程返回一个输出参数,并将该输出参数映射到Entity Framework的.edmx文件中的“Rows Affected Parameter”,那么并发错误也会按预期工作。

This solution (ie using the output parameter) is explained best, I found, here: http://petermannerhult.wordpress.com/2010/10/01/entity-framework-4-with-optimistic-concurrency-and-stored-procedures/ 这个解决方案(即使用输出参数)最好解释,我发现,这里: http//petermannerhult.wordpress.com/2010/10/01/entity-framework-4-with-optimistic-concurrency-and-stored-程序/

Neither of the above steps seem like they should be necessary though, as I would assume that the Entity Framework could just use the number of rows updated to determine if it should raise a concurrency exception. 上述两个步骤似乎都不应该是必要的,因为我认为实体框架可以只使用更新的行数来确定它是否应该引发并发异常。 I've used these exact same stored procedures (with optimistic concurrency) in ADO.NET DataSets, without any problem. 我在ADO.NET DataSet中使用了这些完全相同的存储过程(带有乐观并发),没有任何问题。 So my question is how can I use my existing stored procedures, without modification, to enable optimistic concurrency in the Entity Framework? 所以我的问题是如何在不修改的情况下使用我现有的存储过程来实现实体框架中的乐观并发?

In the Stored Procedure Mapping window of the EDMX designer, there are two check boxes next to each property. 在EDMX设计器的“存储过程映射”窗口中,每个属性旁边都有两个复选框。 Use Original Value and Rows Affected Parameter . Use Original ValueRows Affected Parameter As you noted, the Rows Affected Parameter requires you to use an output parameter in the proc. 如您所述, Rows Affected ParameterRows Affected Parameter要求您在proc中使用输出参数。 However, if you select Use Original Value for the timestamp, it compares them and throws an OptimisticConcurrencyException if the two don't match. 但是,如果为时间戳选择“ Use Original Value ”,则会比较它们并在两者不匹配时抛出OptimisticConcurrencyException This should allow you to get the exception without having to modify the procedure. 这应该允许您在不必修改过程的情况下获取异常。

If you really don't want to change the stored procedure code, you can compare the Timestamp returned from the stored procedure to the Timestamp passed into it. 如果您确实不想更改存储过程代码,则可以将从存储过程返回的Timestamp与传入其中的Timestamp进行比较。 If different then you have a concurrency error. 如果不同则会出现并发错误。

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

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