Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interface and repository abstraction break subqueries in Entity Framework 4.1 DbContext API?

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.

like image 332
GWB Avatar asked May 13 '11 17:05

GWB


2 Answers

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.

like image 171
Michael Teper Avatar answered Nov 11 '22 13:11

Michael Teper


You need to Include all properties to related objects on your DbSet<SomeEntity> before you create an IQuerable out of it.

public class SomeEntity
{
    public Guid Id { get; set; }
    public virtual SomeOtherEntity Other { get; set; }
}

public class Repository
{
    public IQueryable<SomeEntity> Query()
    {
        _context.Set<SomeEntity>().Include("Other").AsQueryable();
    }
}

You can provide this to your Query method by using a func or something. Use your creativity ;)

like image 37
Marco Avatar answered Nov 11 '22 14:11

Marco