After blogging about pessimistic concurrency and the usage of locks in NHibernate yesterday, I wanted to write a follow up on how to do the same thing in Entity Framework Core. Turns out that EF Core does not support Pessimistic Concurrency…
However this does not mean it is not possible. We’ll have to throw some raw SQL in the mix to get it working.
First we need to explicitly create a database transaction:
After doing that we need to set the lock. We do this by creating a ‘fake’ update statement with a lock statement:
Now we can execute our ‘real’ update using EF Core:
Here is the full code example: