Quantcast
Channel: Entity Framework
Viewing all articles
Browse latest Browse all 10318

Edited Issue: DbQuery to ObjectQuery replacement fails in some situations involving closures [396]

$
0
0
Current implementation of DbQueryVisitor tries to handle cases where the query is closed over variables containing DbQuery values by simply replacing them with the underlying ObjectQuery instances. Obviously, if the query is closed over a variable of type DbSet the assignment will fail.

Following code will throw System.ArgumentException: Object of type 'System.Data.Objects.ObjectQuery`1[AnyEntity]' cannot be converted to type 'System.Data.Entity.DbSet`1[AnyEntity]':

using (var anyContext = new AnyContext())
{
var anySet = anyContext.Set<AnyEntity>();

var query = from x in anyContext.Set<AnyOtherEntity>()
from y in anySet
select new { x, y };

// change the type of the anySet variable to IQueryabe and the exception is gone
// but guess what's the value of the anySet variable now
}

Also changing the value of closed variable doesn't seem right for me (simple query construction now has side effects).

Possible solution is to rewrite Constant->Field(DbSet) expression to Constant(ObjectContext) expression.

Viewing all articles
Browse latest Browse all 10318

Trending Articles