Entity Framework Core. Disable concurrency check for specific statements

Viewed 211

I am using EF Core 5 with .Net 5 and SQL Server as Db. I have a simple model:

public class Model 
{
  public Guid Id { get; private set; }

  public decimal Value { get; private set; }

  public decimal Score { get; private set; }

  [ConcurrencyCheck]
  public StateEnum State { get; private set; }
}

Now I want concurrency check when I am updating Value property, but I don't need it when I update Score property (as it is updated frequently and is always overwritten).

Is there a way to disable concurrency check for some statements?

My reason being that concurrency checks adds additional overhead by fetching SELECT @@ROWCOUNT but in some scenarios it's not needed. Also it raises exception and spams logs, but in case of Score update I don't care about the concurrency.

I come up with 2 solutions which I could implement, but maybe there is a better one:

  1. do try{} catch (DbUpdateConcurrencyException ex) {} while this suppresses the exception from the check, the actual check is still happening and that extra overhead is not gone.
  2. Split Model into 2 classes have Score in a separate class without concurrency check. (Maybe even pointing into the same Db table). While this could work it adds additional complexity to the system, especially when you need both Score and Value. So not ideal as well.
0 Answers
Related