LINQ表达式包含与不同上下文相关联的查询的引用 [英] LINQ expression that contains references to queries that are associated with different contexts

查看:214
本文介绍了LINQ表达式包含与不同上下文相关联的查询的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个查询:

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:


指定的LINQ表达式包含与不同上下文关联的查询的引用。

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




  1. 为什么不允许 LINQ to Entities

  2. 仍然可以使用 LINQ to Entities 另一种方式?

  3. 这将是一个什么工作?

  1. Why is this not allowed with LINQ to Entities?
  2. Is it still possible with LINQ to Entities in another way?
  3. What would be a work around for this?


推荐答案

我想象是因为您正在构建的语句在后台转换为SQL,并在数据库上运行。因为不同的上下文可能来自不同的数据库甚至不同的服务器,所以不能保证在查询服务器时context2中的数据可用于context1。

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.

您可以返回来自每个上下文的数据并转换为IEnumerable,然后执行标准的linq查询,但是您有明确的数据传输和内存中处理的开销,否则数据库引擎将执行此操作。

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.

这篇关于LINQ表达式包含与不同上下文相关联的查询的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆