多数据库同一个方法
Multi database in the same method
我的应用程序有 2 个数据库,Db1
(有 tabledbo.Student
)和 Db2
(有 tabledbo.School
)。我创建了 2 个 AppService 来访问它们
当我试图从他们两个那里获取数据时,它只使用到 Db1
的连接(在当前上下文中找不到 table dbo.School
)。那么如何同时从Db1和Db2获取数据呢
private readonly IStudentAppService _studentAppService;
private readonly ISchoolAppService _schoolAppService;
public BranchAccountController(IStudentAppService studentAppService,
ISchoolAppService schoolAppService)
{
_studentAppService = studentAppService;
_schoolAppService = schoolAppService;
}
public async Task<PartialViewResult> GetStudent(int? id)
{
//Repository 1 (Database 1)
var student = await _studentAppService.GetStudentForEdit(new NullableIdDto { Id = id });
//Repository 2 (Database 2)
var school = await _schoolAppService.GetSchoolList();
//bla bla
}
Update 1:
I tried to get the school before student and face the below error:
The transaction passed in is not associated with the current
connection. Only transactions associated with the current connection
may be used.
你必须Begin
分开工作单元:
public async Task<PartialViewResult> GetStudent(int? id)
{
List<SchoolDto> schools;
StudentDto student;
using (var uow = UnitOfWorkManager.Begin(TransactionScopeOption.Suppress))
{
// Repository 1 (Database 1)
student = await _studentAppService.GetStudentForEdit(new NullableIdDto { Id = id });
uow.Complete();
}
using (var uow = UnitOfWorkManager.Begin(TransactionScopeOption.Suppress))
{
// Repository 2 (Database 2)
schools = await _schoolAppService.GetSchoolList();
uow.Complete();
}
// ...
}
我的应用程序有 2 个数据库,Db1
(有 tabledbo.Student
)和 Db2
(有 tabledbo.School
)。我创建了 2 个 AppService 来访问它们
当我试图从他们两个那里获取数据时,它只使用到 Db1
的连接(在当前上下文中找不到 table dbo.School
)。那么如何同时从Db1和Db2获取数据呢
private readonly IStudentAppService _studentAppService;
private readonly ISchoolAppService _schoolAppService;
public BranchAccountController(IStudentAppService studentAppService,
ISchoolAppService schoolAppService)
{
_studentAppService = studentAppService;
_schoolAppService = schoolAppService;
}
public async Task<PartialViewResult> GetStudent(int? id)
{
//Repository 1 (Database 1)
var student = await _studentAppService.GetStudentForEdit(new NullableIdDto { Id = id });
//Repository 2 (Database 2)
var school = await _schoolAppService.GetSchoolList();
//bla bla
}
Update 1: I tried to get the school before student and face the below error: The transaction passed in is not associated with the current connection. Only transactions associated with the current connection may be used.
你必须Begin
分开工作单元:
public async Task<PartialViewResult> GetStudent(int? id)
{
List<SchoolDto> schools;
StudentDto student;
using (var uow = UnitOfWorkManager.Begin(TransactionScopeOption.Suppress))
{
// Repository 1 (Database 1)
student = await _studentAppService.GetStudentForEdit(new NullableIdDto { Id = id });
uow.Complete();
}
using (var uow = UnitOfWorkManager.Begin(TransactionScopeOption.Suppress))
{
// Repository 2 (Database 2)
schools = await _schoolAppService.GetSchoolList();
uow.Complete();
}
// ...
}