I am having some issues doing joins in EF using my service layer. 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“
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.
You are referencing two different repositories in your query. I guess each of them is wrapping an 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.