// here, sometimes getting old data or actual data but conflict updates, on multiple calls at the same time
var readyProduct = await _readyProductRepository.FindAsync(p => p.ModelId == readyProductCreate.ModelId);
if (readyProduct == null)
{
readyProduct = _mapper.Map<ReadyProductCreate, ReadyProduct>(readyProductCreate);
await _readyProductRepository.AddAsync(readyProduct);
}
else
{
readyProduct.Count += readyProductCreate.Count;
_readyProductRepository.Update(readyProduct);
}
var transaction = new ReadyProductTransaction
{
ModelId = readyProductCreate.ModelId,
Count = readyProductCreate.Count,
Status = ReadyProductTransactionType.Import,
Date = DateTime.Now,
};
await _transactionRepository.AddAsync(transaction);
await _unitOfWork.SaveAsync();
我用隔离级别Serializable
在Transaction
内覆盖了整个过程,但它达到了deadlock
.
也许,在插入ReadyProductTransaction
表之后创建trigger
以更新ReadyProduct
表将是一种解决方案,但我想知道是否可以不使用触发器.
DbContext-注册为作用域
所有存储库类,包括UnitOfWork类-注册为作用域
应用程序-.NET5.0
运行在- IIS上的应用程序