Error: "The Specified Linq Expression Contains References to Queries That Are Associated with Different Contexts"

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

You'll have to perform two database queries:

var IDs =  (from a in db1.Table1 
join b in db1.Table2 on a.Id equals b.Id
orderby a.Status
where b.Id == 1 && a.Status == "new"
select new a.Id).ToArray();

var query = from c in db2.Company
join a in IDs on c.Id equals a.Id
select new { Id = a.Id, CompanyId = c.CompanyId };

The .ToArray() is crucial. It prevents EF from trying to execute the combined query (which will fail since it uses two different contexts). You can use .AsEnumerable() if you'd rather keep lazy loading.


And your follow-up question:

Is there any other way to make the LINQ query more optimized? That is,
to perform the action in a single LINQ query itself?

In order for your original query to successfully run, it must use only a single data context, which means all the data must be available from a single EDMX, which in turn means a single connection string. There are several ways you can achieve that:

  • If both tables are on the same database, add them both to a single EDMX.
  • If they're on different databases but on the same instance, create a view on one of the databases that selects from the table on the other database, then add the local table and view to a single EDMX.
  • If they're on different instances/servers, created a linked server, then create a view of the table on the linked server, then add the local table and view to a single EDMX.

Error: “The specified LINQ expression contains references to queries that are associated with different contexts”

The linq to entities query gets translated in to sql and it gets executed in the sql server, the above code is using two different context instances in single query i.e. UserManager and db and the framework does not allow having two different database contexts in a single query.

you can fetch Users and Departments separately in memory and then organize them in one collection or you would need to use same context to query the data (using db for both Users and Departments) and then you can fetch both results in single request to database server.

Hope it helps.

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

MyStrings is a small table

Load filtered MyStrings in memory, then join with MyStaticStringTranslations using LINQ:

// Read the small table into memory, and make a dictionary from it.
// The last step will use this dictionary for joining.
var byId = db1.MyStrings
.Where(x => homeStrings.Contains(x.Content))
.ToDictionary(s => s.Id);
// Extract the keys. We will need them to filter the big table
var ids = byId.Keys.ToList();
// Bring in only the relevant records
var myStrings = db2.MyStaticStringTranslations
.Where(y => ids.Contains(y.id))
.AsEnumerable() // Make sure the joining is done in memory
.Select(y => new {
Id = y.id
// Use y.id to look up the content from the dictionary
, Original = byId[y.id].Content
, Translation = y.translation
});

The specified LINQ expression contains references to queries that are associated with different contexts error in c#

You can't combine in one query data retrieved from multiple contexts, these multiple contexts may point to two difference databases or have different options, then the query might not execute.

It seems that each repository creates its own DataContext object and retrieves the data using that object.

To solve that without have to pass the context and share it between all repositories, you might want to return a List from the Get() method and not an IQueryable. I don't know if you think that Get() is executing the query but it is not. It is just storing information about the query in IQueryable. Only when you call .ToList(); inside your ShowSheet method, the queries of Get() gets executed.

Else, you need to create one context and use it in all the repositories in this query. Your option here is to have the constructor of the repository (or the Get() Method itself) accept a YourContext parameter. Then you pass the context : _sheetRepository.Get(context) so that all of them share the same object.

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

Have a look at implementing the unit of work pattern so that you can share a context instance across repositories.

There are many examples on the web, e.g. http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

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

I think that's because your entities which you created in the plugin contains a relation between Nop.Core entity such as Product

Entity Framework does not support cross-project relations You have to remove the Nop.Core Entity such as (Product property) from your entity and use ProductId int instead.



Related Topics



Leave a reply



Submit