dotnet / EntityFramework.Docs

Documentation for Entity Framework Core and Entity Framework 6
https://docs.microsoft.com/ef/
Creative Commons Attribution 4.0 International
1.62k stars 1.96k forks source link

Clarify requirements for optimistic concurrency when using stored procs #1101

Open AAATechGuy opened 5 years ago

AAATechGuy commented 5 years ago

In the example of "class and update stored procedure with a timestamp concurrency token", it fails with ConcurrencyException - "Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=472540 for information on understanding and handling optimistic concurrency exceptions.". On debugging, it was found that UPDATE PROC must return/select updated Timestamp, else fails with concurrency. You need to add the following to the PROC to make it work successfully.

select [Timestamp] from [dbo].[Blogs] WHERE BlogId = @BlogId


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

AAATechGuy commented 5 years ago

Also, note, after adding the following and if RowCount is not returned as an output parameter, RowCount validation is not done, which is dangerous as optimistic concurrency is not done in EF.

select [Timestamp] from [dbo].[Blogs] WHERE BlogId = @BlogId

Maybe its intentional, but, I was surprised on why my entity update went through with invalid data.