I am using EF 6 and Code First
database solution in my application. The same application runs on several computers and access the same database. One Integer field in database is updated from these applications, the value of this field is reduced. The code is below and I think here could be race condition problem. How do you solve the problem in this situation?
public partial class CaContext
{
public override int SaveChanges()
{
var addedStatistics = ChangeTracker.Entries<Statistic>().Where(e => e.State == EntityState.Added).ToList().Select(p => p.Entity).ToList();
var testOrders = GetUser.Orders.First();
testOrders.Credits = testOrders.Credits - addedStatistics.Count; //Race condition here
return base.SaveChanges();
}
}