'How to increment a value using one command using entity framework

How can i transform this sql query to an EF linq command

"update dbo.table set col1= col1 + 1 where Id = 27" 

i want to execute this query using one command to avoid concurrency problems in case of another client modify the record in the same time

i'm looking for doing that using EF but in one command

i tried this but i'm looking for a better solution :

context.table1.FromSqlInterpolated($"update dbo.table  set col1= col1+ 1 where Id=27").FirstOrDefaultAsync();


Solution 1:[1]

I would propose to use linq2db.EntityFrameworkCore (note that I'm one of the creators)

Then you can do that with ease:

await context.table1.Where(x => x.Id == 27)
   .Set(x => x.Col1, prev => prev.Col1 + 1)
   .UpdateAsync();

Solution 2:[2]

Even the original SQL statement should be executed within a transaction if you want to be sure no other changes can occur between reading and updating the value. It's one SQL statement, but the db still has to read the value, increment and store.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Svyatoslav Danyliv
Solution 2 pjs