开发者

LINQ expression that contains references to queries that are associated with different contexts

开发者 https://www.devze.com 2023-03-27 01:49 出处:网络
I have this query: var list = (from t1 in context1.SomeTable join t2 in context2.SomeTable on t1.ID equals t2.ID

I have this query:

var list = (from t1 in context1.SomeTable
            join t2 in context2.SomeTable on t1.ID equals t2.ID
            where //some where clause
            select new { t1.SomeField, t2.SomeField }).ToList());

I will get this error when this query tries to execute:

The specified LINQ expression contains references to queries that are associated with different contexts.

  1. Why is this not allowed with LINQ to Entities?
  2. Is it still possible with L开发者_高级运维INQ to Entities in another way?
  3. What would be a work around for this?


I'd imagine it's because the statement you're building up is converted to SQL behind the scenes and run on the database. Because different context could come from different databases or even different servers, there's no guarantee that the data in context2 is available to context1 when the server is being queried.

You could return the data from each context and convert to IEnumerable and then perform standard linq queries then but you've got a clear overhead of data transfer and in-memory processing that would have otherwise been performed by the database engine.

0

精彩评论

暂无评论...
验证码 换一张
取 消