EF:交叉edmx(相同的数据库)linq-to-entites查询 [英] EF: Cross edmx (same DB) linq-to-entites query

查看:156
本文介绍了EF:交叉edmx(相同的数据库)linq-to-entites查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在单独的程序集中使用两个EDMX,而不是在同一个数据库中,
创建一个使用它们的LINQ到实体的查询?



例如



这是我要做的:

 $($)
{
var items2 = context2.Items .Where(item2 => item2.Color ==Red);

var query = context1.Items.Where(item =>
items2.Any(item2 => item2.PainterId == item.PainterId));
}
}

 >这将导致NotSupportedException。 >
  消息:指定的LINQ表达式包含对与不同上下文相关联的查询的引用。



 >即使替换了Entities2,此异常也是抛出与实体1

   (即使两个上下文都是相同的EDMX),并且都使用相同的连接字符串。







比较,另一方面工作和结果在一个单一的SQL语句:

  using(var context1 = new Entities1()) 
{
var items2 = context2.Items.Where(item2 => item2.Color ==Red);

var query = context1.Items.Where(item =>
items2.Any(item2 => item2.PainterId == item.PainterId));
}



限制条件:



我的意图是使用两个EDMX设计器支持 - 不要以破坏设计器的方式攻击EDMX,或者从数据库更新时被覆盖。 / p>

EDMX#1无法知道EDMX#2(但#2可以知道#1)。



我希望结果转换为单个SQL查询,而不是从第一部分到内存中读取结果,然后将其作为第二部分查询的输入返回到数据库。








相关,但不是我要找的:




解决方案

你以你的问题的方式限制了你的要求:不可能。最好的唯一推荐的解决方案是在第二个链接中引用ADO.NET团队博客关于使用大型模型的文章。



我写了关于一个黑客(并且我成功地在一个项目中使用),但是它还有一个缺点 - 您必须对两个EDMX使用单个上下文。即使这样做,我也不建议使用这种方式,因为它可能会有未开发的缺点,因为它内部省略了EF中许多其他地方使用的容器名称。


How can I use two EDMXs, in separate assemblies, yet above the SAME database,
to create a linq-to-entities query that uses them both?

E.g.

This is what I am trying to do:

using (var context1 = new Entities1())
{
    using (var context2 = new Entities2())
    {
        var items2 = context2.Items.Where(item2 => item2.Color == "Red");

        var query = context1.Items.Where(item =>
            items2.Any(item2 => item2.PainterId == item.PainterId));
    }
}

 > This results in NotSupportedException.
   Message: "The specified LINQ expression contains references to queries that are associated with different contexts."

 > This exception is throw even if Entities2 is replaced with Entities1
   (even if both contexts are from same EDMX) and both using the same connection string.



For comparison, this on the other hand works and results in a single SQL statement:

using (var context1 = new Entities1())
{
    var items2 = context2.Items.Where(item2 => item2.Color == "Red");

    var query = context1.Items.Where(item =>
        items2.Any(item2 => item2.PainterId == item.PainterId));
}


Constraints:

My intent is to use two EDMXs WITH designer support - no hacking EDMX in a way that breaks designer or that gets overwritten when updating from database.

EDMX #1 can not know about EDMX #2 (however #2 can know about #1).

I want the result to translate to a single SQL query, not to read results from first part to memory, then return them to database as an input for second part of query.



Related, but not what I am looking for:

解决方案

You constrained your requirements in the way that answers your question: No it is not possible. The best and only recommended solution is in the second link which references ADO.NET team blog's article about working with large models.

I wrote about a hack (and I successfully used in one project) which works as well but it has another disadvantage - you must use single context for both EDMXs. Even it worked I don't recommend using that way because it can have unexplored disadvantages because it internally omits container name which is used in many other places in EF.

这篇关于EF:交叉edmx(相同的数据库)linq-to-entites查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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