简体   繁体   English

实体框架4.3,使用联接

[英]Entity Framework 4.3, using joins

I am having some issues doing joins in EF using my service layer. 我在使用服务层进行EF联接时遇到一些问题。 Whenever I try to do a join I get the following error: 每当我尝试进行加入时,都会出现以下错误:

" The specified LINQ expression contains references to queries that are associated with different contexts " 指定的LINQ表达式包含对与不同上下文关联的查询的引用

I have spent countless hours trying to figure this out. 我花了无数小时试图解决这个问题。 Most of the articles I have seen deal with joining entity sets from different contexts. 我见过的大多数文章都涉及来自不同上下文的联接实体集。 However this is happening to me using the same DB. 但是,这是使用同一数据库发生的。 What am I doing wrong? 我究竟做错了什么?

Example code: 示例代码:

 public virtual IList<ProductVariantAttribute> GetProductVariantAttributes(int ProductID)
     {
         var query = from pva in _productVariantAttributeRepository.Table
                     join b in _productAttributeRepository.Table on pva.ProductAttributeID equals b.ProductAttributeID
                     where pva.ProductID == ProductID
                     select pva;

         var productVariantAttributes = query.ToList();
        return productVariantAttributes;
    }

This has nothing to do with databases. 这与数据库无关。 It has everything to do with instances of ObjectContext . 它与ObjectContext的实例有关

You are referencing two different repositories in your query. 您在查询中引用了两个不同的存储库。 I guess each of them is wrapping an EntityContext. 我想每个人都包装一个EntityContext。 This will cause this message. 这将导致此消息。

It is also an anti-pattern. 这也是一种反模式。 I recommend that you open exactly one EntityContext per HTTP-request or per WCF call or whatever your unit of work is. 我建议您为每个HTTP请求或每个WCF调用或您的工作单位完全打开一个EntityContext。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM