1

I want to do this query to bypass concurrency conflicts on Balance column.

Update SomeTable
set Balance = Balance + 500
where Id = someId

I know one solution is to use DbContext.Database.ExecuteSqlCommand() and another solution is to use Optimistic Concurrency Check.

Is there any way to do the query using EF Core (nor ExecuteSqlCommand neither optimistic concurrency check)?

1
  • 1
    I would do this with linq2db. If you are not ready to give up EF Core right now, then take this add-on to it. Jan 30, 2021 at 10:25

2 Answers 2

2

I think EFCore.BulkExtensions library should be able to solve your problem. and other more powerful library Z.EntityFramework.Plus.EFCore.

EFCore.BulkExtensions:

// Delete
context.Items.Where(a => a.ItemId >  500).BatchDelete();
context.Items.Where(a => a.ItemId >  500).BatchDeleteAsync();

// Update (using Expression arg.) supports Increment/Decrement 
context.Items.Where(a => a.ItemId <= 500).BatchUpdate(a => new Item { Quantity = a.Quantity + 100 });
  // can be as value '+100' or as variable '+incrementStep' (int incrementStep = 100;)
  
// Update (via simple object)
context.Items.Where(a => a.ItemId <= 500).BatchUpdate(new Item { Description = "Updated" });
context.Items.Where(a => a.ItemId <= 500).BatchUpdateAsync(new Item { Description = "Updated" });
// Update (via simple object) - requires additional Argument for setting to Property default value
var updateColumns = new List<string> { nameof(Item.Quantity) }; // Update 'Quantity' to default value('0'-zero)
var q = context.Items.Where(a => a.ItemId <= 500);
int affected = q.BatchUpdate(new Item { Description = "Updated" }, updateColumns);//result assigned to variable

reference: EF Core Tools & Extensions

1

You are not really bypassing concurrency conflicts by executing the query.

EF Core implements optimistic concurrency control, meaning that it will let multiple processes or users make changes independently without the overhead of synchronization or locking.

This means that ef core objects will update in memory without locking until you call save changes.

So, whether or not you execute the query, it will not stop anyone attaching the object and update it at the same time.

By the way, optimistic concurrency is the default ef setting.

3
  • You said that the query above doesn't bypass concurrency conflicts, I don't understand why this happens. This sql command is running in atomic way. Somehow dbms handle this, doesn't it? Jan 30, 2021 at 10:55
  • 1
    Imagine the following scenario. You load an object with ef core. Then you execute a query that deletes the row. This is an atomic operation and the value is updated. Now you change the object in memory and try to save it. The in memory change does not interact with the database. The save operation will interact with the database and now the object does not exist so you have a conflict. Jan 30, 2021 at 12:12
  • 1
    If it were about two queries in the db, then indeed the first one would lock and the second one would wait. Jan 30, 2021 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.