TransactionScope throwing exception I am using .net core 2.2
In this example I am first creating scope of TransactioScop then opening SQL transaction for one database
.NET Core doesn't support Distributed Transactions because it would require a different transaction manager on each platform. It may appear in the future (here's the issue in-progress), but for now any Transaction that would require two different resource managers will throw this exception.
Instead you coordinate separate transactions. Have two seperate transactions complete their work, and then commit them both. There is a possibility that the first one succeeds and the second one fails, but for SQL Server, that would be a very rare occurance. Something like:
_db1UOW.Begin(); //creating sql transaction
await _db1UOW.IDenialDetailsRepositorydb1.InsertDenialDetails(denialsDetails);
await _db1UOW.IRuleDetailsRepositorydb1.InsertRulesDetails(rulesDetails);
_db2UOW.Begin(); //creating sql transaction
await _db2UOW.IRuleDetailsRepository.GetRulesDetails();
await _db2UOW.IDenialDetailsRepository.InsertDenialDetails(denialsDetails);
var data = await _db2UOW.IRuleDetailsRepository.InsertRulesDetails(rulesDetails);
_db1UOW.Commit(); //commitng sql transaction
try
{
_db2UOW.Commit(); //commitng sql transaction
}
catch (Exception ex)
{
LogError("Second transaction failed to commit after first one committed. Administrators may need to fix stuff");
throw;
}
Or if the two databases are on the same server you can use cross-database queries to enlist the changes in a single SQL Server transaction.