How to avoid pessimistic locking with EF Core

This title was summarized by AI from the post below.
View profile for Milan Jovanović
Milan Jovanović Milan Jovanović is an Influencer

Practical .NET and Software Architecture Tips | Microsoft MVP

EF Core doesn't support pessimistic locking out of the box. So, what can you do if you need this feature? I'm sorry (no, I'm not) to have to say this, but you'll have to write SQL. Pessimistic locking is a form of concurrency control where your transactions lock the queried rows until the transaction is committed or rolled back. Unfortuntalely, you can't easily do this with EF Core. The solution? Just write some SQL. Postgres has the SELECT FOR UPDATE statement. SQL Server has the UPDLOCK query hint. Tomorrow, I'll show 46,427+ engineers how to implement pessimistic concurrency control with EF Core. You can subscribe here: https://bit.ly/3x8W1dU P.S. This could be the missing insight you need to stop those pesky concurrency bugs.

  • diagram
Milan Jovanović

Practical .NET and Software Architecture Tips | Microsoft MVP

1y

𝗣.𝗦. If you liked this, consider joining The .NET Weekly - my newsletter with 46,000+ engineers that teaches you how to improve at .NET and software architecture. Subscribe here → https://bit.ly/3x8W1dU

Daniel Moka

I help you craft better software

1y

Raw SQL is the most understandable in any case. EF core has great support for it!

Saeed Esmaeelinejad

Microsoft MVP | a Developer | .NET Enthusiast & Content Creator

1y

Great topic, Looking forward to the newsletter 👌

Mnac Margaryan

Technical Lead Software Engineer, Tutor

1y

Check out this nuget (source code available) its an easy and intuitive way of using postgres lock in ef core. You just need to call ForUpdate() method on IQueryable and thats all. https://www.nuget.org/packages/Pandatech.EFCore.PostgresExtensions

Rafsanul Hasan

Senior Software Engineer L-2 @ Streams Tech | Full Stack | .NET Developer | JavaScript / Typescript Developer | Architect | DevOps Process Automation Engineer | MCSD | A-CSD | ACP & Agile Coach

1y

Yeah EF has a lot of limitations but still I don't want to write much SQL. I've found a way to write "For Update" with select queries in PostgresSQL using LINQ. I'll write a post about it soon.

Pavle Davitković

Microsoft MVP | Daily .NET topics to help you stay up to date

1y

Haha, the visuals intrigue me 😁

Brandon Shaw

Senior software engineer specializing in back-end service API's utilizing C#. | Currently focusing on financial systems. | Senior Developer Level 1 at Derivco

1y

I have to do this on my work api in order to sequence concurrent calls for financial updates

Riguel Figueiró

Senior Software Engineer | Software Architect | @Gauge | Cloud & Platform Engineering | AWS | .NET | Node.js | Typescript

1y

Great Milan Jovanović. I'm currently working at a company that has many related topics on this subject. I look forward to your message

M'BARK BELKHDAR

.NET Software Engineer & Trainer

1y

Wait, don't SQL Server and other RDBMS using the locking mechanism by default ?

Like
Reply
Manish Jaiswal

Senior Engineer @Stryker | Ex- Nagarro | .NET | Angular | System Design | Problem Solving | SQL Server | 1 X GCP

1y

Thanks for the info! The pessimistic lock is really important to handle concurrency, especially shared lock 😀

Like
Reply
See more comments

To view or add a comment, sign in

Explore content categories