Reputation: 10779
I am working on a project which is functioning on .netframework 4.6, VS 2015, EntityFramework 6.x
I am trying to connect to more than one database which are in different database servers.
My dabatase looks like :
I have a main project and repository project which is a class library.
In my repository project I added two EntityFramework models namely CompanyEmployeeModel.edmx and MultipleTestModel.edmx.
In My EmployeeDAL.CS :
private static async Task<string> GetCompanyInformationByIdCompany(int idCompany)
{
string result = string.Empty;
try
{
using (var db = new Entities())
{
var companyInformation = await db.CompanyInformations.Where(x => x.id == idCompany).FirstOrDefaultAsync();
result = companyInformation.name;
}
using (var dbTest = new fccidevEntities())
{
var EmployeeInformation = await dbTest.Employees.Where(x => x.Id == 10).FirstOrDefaultAsync();
//result = EmployeeInformation..name;
}
}
catch
{
result = string.Empty;
}
return result;
}
Is this the right way of doing. Can somebody suggest how to connect to multiple databases residing in multiple servers.
Exception :
Upvotes: 0
Views: 107
Reputation: 709
With EF you'll need two contexts, there's no way of getting rid of this, what I would do in your case is to have something encapsulating the contexts.
This way you'll have not to worry what database are you acessing at this moment.
Just be aware that for most requests you'll open two differente connections and that comes at a cost, you'll have more network IO and memory load than a single database application.
Upvotes: 2