多个同时连接DotNetCore

时间:2019-12-23 22:43:33

标签: mysql .net-core transactionscope n-tier-architecture

我想在我的dotnet核心项目中将TransactionScope用于我的业务服务。 我同时使用MysqlConnection和SqlConnection。 我的目的是在自己的数据库中添加一些数据,并在客户端数据库上执行一些操作。

 using TransactionScope scope = new TransactionScope();
 var addDatabase = await _databaseRepository.AddDatabase(mappedEntity);
 await _realDbService.CreateDatabaseOnRemote(dto);
 scope.Complete();

这是我服务的代码示例。 每个存储库都执行自己的操作。 我试图关闭连接。

但是我遇到一个错误,即当前不支持多个同时连接或同一事务中具有不同连接字符串的连接。

感谢您的评论。

1 个答案:

答案 0 :(得分:0)

我针对此问题使用了解决方案,但不确定是否为最佳解决方案。

我使用了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();