我想在我的dotnet核心项目中将TransactionScope用于我的业务服务。我同时使用MysqlConnection和SqlConnection。我的目的是在自己的数据库中添加一些数据,并在客户端数据库上执行一些操作。
using TransactionScope scope = new TransactionScope();
var addDatabase = await _databaseRepository.AddDatabase(mappedEntity);
await _realDbService.CreateDatabaseOnRemote(dto);
scope.Complete();
这是我服务的代码示例。每个存储库都执行自己的操作。我试图关闭连接。
但是我有一个错误,当前不支持多个同时连接或同一事务中具有不同连接字符串的连接。
感谢您的评论。
我为这个问题使用了解决方案,但是我不确定这是最佳解决方案。
我使用Transaction.Current.TransactionCompleted事件,如果事务完成,那么我将我的方法内联到一个范围内。
using TransactionScope scope = new TransactionScope();
addDatabaseResult = await _databaseRepository.AddDatabase(mappedEntity);
Transaction.Current.TransactionCompleted += delegate
{
using TransactionScope scopeInline = new TransactionScope();
_realDbService.CreateDatabaseOnRemote(dto);
scopeInline.Complete();
};
scope.Complete();