0

My app has 2 databases, Db1 (has table dbo.Student) and Db2 (has table dbo.School). I created 2 AppService to access them When I tried to get data from both of them, It only uses the connection to Db1 (Could not found table dbo.School in the current context). So how can I get the data from Db1 and Db2 at the same time

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.

Hoang Tran
  • 886
  • 3
  • 13
  • 32

1 Answers1

2

You have to Begin separate units of work:

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();
    }

    // ...
}
aaron
  • 39,695
  • 6
  • 46
  • 102