Question

I am using 2 entity frameworks. One is project database and other one is employee database. I am storing project manager's employee id on Project database table, but when I am dispalying I have to join the employee table from employee DB to show the employee name not the employee id.

How to do join two entities.

Was it helpful?

Solution

There are a few diferent ways to acomplish this.

You can create a view on one of the databases that joins the two tables from different dbs, then it will be one object in Entity Framework.

You can also get the objects from each table you want to join as in-memory objects first that are disconnected from EF. Obviously you want to filter as much as possible on the db side, before pulling the table/results into memory. Then you can join the disconnected objects

var projects = db1.projects.Where(p=> /** filter here **/).Select(p=> /** select new anonymous type **/).ToList()

var employees = db2.employees.Where(e=> /** filter here **/).Select(e=> /** select new anonymous type **/).ToList()

from p in projects
join e in employee
on p.managerid equals e.employeeid
select new { EmployeeId = e.EmployeeId, Project = p.ProjectName };
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top