I have the following code where I am using TransactionScope and UnitOfWork pattern which is developed using asp.net core 3.1 and C#.
publc void TestMethod()
{
var dto = _mapper.Map<Disclaimers>(request);
using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
dto.CreatedBy = "Test1";
dto.CreatedDate = DateTime.UtcNow;
dto.UpdatedBy = "Test2";
dto.UpdatedDate = DateTime.UtcNow;
await _testRepository.AddAsync(dto);
await _testRepository.UnitOfWork.SaveEntitiesAsync();
response.IsSuccessful = true;
scope.Complete();
}
}
I wanted to know is it ok or good practice to leverage both TransactionScope and UnitOfWork in the same method.
Can anyone help me here by providing their guidance?
In all versions of Entity Framework, whenever you execute SaveChanges() to insert, update or delete on the database the framework will wrap that operation in a transaction.
If you have just one SaveChanges
and one DbContext
, then there is no need to use TransactionScope