In my ASP.Net MVC application, there are two contexts I have linked with my system.
In my controller, I have mentioned it as
private zSqlDb db = new zSqlDb();
private DatabaseZSql dbs = new DatabaseZSql();
So I want to connect some table from both contexts and I wrote this code to link it and get the data.
var EmpDetails = (from e in db.CreateEmployee
join dep in db.Department on e.DepId equals dep.Id
join des in db.Designation on e.DesignId equals des.Id
join emDetails in dbs.EmpDetails on e.Id equals emDetails.EmpID
join supervisor in db.Employee on emDetails.EmpID equals supervisor.Id
where e.Id == UId select new {
e.Id,
e.EmpNo,
e.EmpName,
dep.Department,
des.Designation,
emDetails.BasicSalary,
emDetails.EmpCatagory,
emDetails.EmpGrade,
emDetails.YearOfService,
SupervisorName = supervisor.EmpName
});
When I debug the code I got an error “`The specified LINQ expression contains references to queries that are associated with different contexts.“
2
Answers
Thanks to @YongShun and @mdfariduddinkiron. I wrote this and this worked
Linq
does not allow you toquery
tables that are from differentcontexts
So try to fetch all the related data from one context first. Then query that data with the second context. The
.AsEnumerable()
part is crucial.For more information visit This question