I am doing project in ASP.NET Core MVC with Entity Framework Core.
I have two databases as contexts and I need to push to index this SQL:
SELECT
*
FROM
db1.table1 AS tpt
JOIN
db2.table1 AS ta ON tpt.cIBAN = ta.cIBAN
JOIN
db2.table2 AS tpa ON ta.nAccountID = tpa.nAccountID
JOIN
db2.table3 AS tc ON tc.nClientID = ta.nAccountID
WHERE
ta.lActive = 1;
What I made so far is this
var query = await (from tpt in context1.table1
join ta in context2.table1 on tpt.CIban equals ta.CIban
join tpa in context2.table2 on ta.NAccountId equals tpa.NAccountId
join tc in context2.table3 on ta.NAccountId equals tc.NClientId
where ta.LActive == true
select new
{
tpt.DRegister,
tc.CNameOnPaymentCard,
tc.NClientId,
tpt.CIban,
tpt.CCurrency,
ta.NCurrency,
tpt.DValidityFrom,
tpt.DValidityTo,
tpt.CNote
}).ToListAsync();
However this will not work as I can not use query on two contexts at the same time.
Would really appreciate any help. Thanks, it bothers me for some time now....
EDIT:
I fixed the problem by splitting query into two LINQ statements returning lists and then concatenating two lists into one list of class. Not best solution, its hideous and slow. However, its working.
Solution:
var x = context1.table1.ToList();
var y = (from ta in context2.table1
join tc in context.table2 on ta.NAccountId
equals tc.NClientId
where ta.LActive == true
select new {
thingsI_NeededToList
...
}).ToList();
var xy = from table1 in y
join table2 in x on table1.CIban equals table2.CIban
select new ClassINeededToPass
{
ClassParameter = table1.parameter
...
};