Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5995759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:00:24+00:00 2026-05-23T00:00:24+00:00

The goal: I’m trying to use the new Entity Framework 4.1 DbContext API (using

  • 0

The goal:
I’m trying to use the new Entity Framework 4.1 DbContext API (using Database First with the new ADO.NET DbContext Generator for the POCO classes) and provide a layer of abstraction using basic, generic repositories.

The problem:
If I try to use a subquery with my repositories, EF cannot complete the translation and throws an error:
System.NotSupportedException: LINQ to Entities does not recognize the method ‘System.Linq.IQueryable`1[EntityFramework41Test.Data.Entity.Table2] Query()’ method, and this method cannot be translated into a store expression.

I’ve successfully used this design with the old 4.0 ObjectContext in the past, but I’d like to use the new API. The same approach also fails with the 4.0 ObjectContext API (tested using generated POCO entities).

Note: I don’t think it’s realistic to post hundreds of lines of code but I have a sample solution with an ASP.NET MVC 3 project using SQL Server CE 4.0 and a basic unit test project demonstrating the outcome of various approaches that can be uploaded or emailed if that helps.


The repository interface I am using is dead simple:

public interface IRepository<TEntity> : IDisposable where TEntity : class, ITestEntity
{
    TEntity GetById(int id);
    IQueryable<TEntity> Query();
    void Add(TEntity entity);
    void Remove(TEntity entity);
    void Attach(TEntity entity);
}

The context interface is even more simple:

public interface ITestDbContext : IDisposable
{
    IDbSet<TEntity> Set<TEntity>() where T: class, ITestEntity;
    void Commit();
}

Here is the sample usage that does not work, using interfaces for the repository and context instances:

using (ITestDbContext context = new TestDbContext())
using (IRepository<Table1> table1Repository = new Repository<Table1>(context))
using (IRepository<Table2> table2Repository = new Repository<Table2>(context))
{
    // throws a NotSupportedException
    var results = table1Repository.Query()
        .Select(t1 => new
        {
            T1 = t1,
            HasMatches = table2Repository.Query()
                .Any(t2 => t2.Table1Id == t1.Id)
        })
        .ToList();
}

The code above is the approach I’d like to use. The concrete classes will end up being injected.

Please disregard the fact that there are better ways to write this particular query than using a subquery. I’ve purposely simplified the code to focus on the actual issue: EF won’t translate the query.

Storing the “inner” repository Query() method result in a local variable actually does work, but is not ideal as you’d have to remember to do it all the time.

using (ITestDbContext context = new TestDbContext())
using (IRepository<Table1> table1Repository = new Repository<Table1>(context))
using (IRepository<Table2> table2Repository = new Repository<Table2>(context))
{
    var table2RepositoryQuery = table2Repository.Query();

    // this time, it works!
    var results = table1Repository.Query()
        .Select(t1 => new
        {
            T1 = t1,
            HasMatches = table2RepositoryQuery
                .Any(t2 => t2.Table1Id == t1.Id)
        })
        .ToList();
}

I’ve also noticed some other approaches break or succeed, e.g. disregarding the repositories and calling TestDbContext.Set<TEntity>() works but ITestDbContext.Set<TEntity>() won’t translate. Changing the definition of ITestDbContext.Set<TEntity>() to return DbSet<TEntity> instead of IDbSet<TEntity> still fails.

Edit:
I don’t think this is possible without some query interception and translation. If I do find a solution in the future, I’ll be sure to share it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T00:00:24+00:00Added an answer on May 23, 2026 at 12:00 am

    I don’t have experience with EF, but based on working with NHibernate and its evolving LINQ support, I suspect that the answer to your question is the one you won’t like — it sounds like this particular construct is not (yet?) supported by the EF LINQ provider and you need to alter your query.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Goal: Create Photomosaics programmatically using .NET and C#. Main reason I'd like to do
Goal Java client for Yahoo's HotJobs Resumé Search REST API . Background I'm used
My goal is to maintain a web file server separately from my main ASP.NET
The goal: To create a .NET dll i can reference from inside SQL Server
My goal is pretty simple: to use ant to build an EAR which contains
My Goal I would like to have a main processing thread (non GUI), and
My goal here is to create a very simple template language. At the moment,
my goal is to write a stored proc that can collect all field values
My goal is to recognize simple gestures from accelerometers mounted on a sun spot.
My goal is to integrate testing into my development environment (as post-build step). I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.