Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm getting an error when trying to join against multiple tables in a query:

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

It's confusing because it makes it seem like I'm using different contexts within the query but I'm not:

public static IQueryable<Company> GetAll(bool supportsMMAT)
            {
                return from c in Context.Companies
                            join v in Context.Vehicles on c.CompanyIdNumber equals v.CompanyIdNumber
                            join mt in Context.ModemTypes on v.ModemTypeId equals mt.Id
                            where !c.CompanyShutOff
                                && (!supportsMMAT || mt.Model == "MMAT")
                            select c;
            }

Any ideas? I'm using the EF4 CTP5 code first approach, if that makes any difference...

like image 430
Justin Avatar asked Feb 26 '23 13:02

Justin


1 Answers

This can happen if your Context property returns a new instance every time.

like image 61
DamienG Avatar answered Apr 28 '23 19:04

DamienG