Error while querying entities with one A to many B and one C to many B relationships
See the Context snippet below to reproduce this problem, but we've been trying to model and query entities with the following relationships in EF7-rc1:
EntityA has one EntityB and EntityB has many EntityA
EntityC has one EntityB and EntityB has many EntityC
When querying the EntityA with EntityAs.Where(e => e.EntityB.EntityCs.Count() > 0) or EntityC with EntityCs.Where(e => e.EntityB.EntityAs.Count() > 0) we get the following error (with client query evaluation turned on):
System.InvalidOperationException: variable 't0' of type 'Microsoft.Data.Entity.Query.EntityQueryModelVisitor+TransparentIdentifier`2[Microsoft.Data.Entity.Storage.ValueBuffer,Microsoft.Data.Entity.Storage.ValueBuffer]' referenced from scope '', but it is not defined
With client query evaluation turned off, we get the following:
System.InvalidOperationException: Unabled to compile the LINQ expression 'join EntityB e.EntityB in value(Microsoft.Data.Entity.Query.Internal.EntityQueryable`1[UnitTestProject1.EntityB]) on Property([e], "EntityBId") equals Convert(Property([e.EntityB], "Id"))' because it requires client evaluation, which is disabled. Either enable client evaluation or rewrite the query to not require client evaluation.
Our DbContext looks like this:
#region Domain
public class EntityA
{
public Guid Id { get; set; }
public EntityB EntityB { get; set; }
}
public class EntityB
{
public Guid Id { get; set; }
public ICollection<EntityA> EntityAs { get; set; }
public ICollection<EntityC> EntityCs { get; set; }
}
public class EntityC
{
public Guid Id { get; set; }
public EntityB EntityB { get; set; }
}
#endregion
public class TestContext : DbContext
{
public DbSet<EntityA> EntityAs { get; set; }
public DbSet<EntityB> EntityBs { get; set; }
public DbSet<EntityC> EntityCs { get; set; }
public TestContext(DbContextOptions options) : base(options) { }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<EntityA>(entity =>
{
entity
.HasOne(e => e.EntityB)
.WithMany(v => v.EntityAs)
.IsRequired();
});
modelBuilder.Entity<EntityB>();
modelBuilder.Entity<EntityC>(entity =>
{
entity
.HasOne(e => e.EntityB)
.WithMany(v => v.EntityCs)
.IsRequired();
});
}
}
Here's a VS solution that reproduces this problem with unit tests - EFTests.zip
Any help or guidance is appreciated. Thanks!
@maumar possibly already fixed by your latest work?
I wish this is fixed asap in rc2.
@mikary can you verify if this is fixed
Ran the provided samples against Microsoft.EntityFrameworkCore.SqlServer 1.0.0-rc2-final. No exceptions were thrown and the results appear to be correct.
class Program
{
static void Main(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder();
optionsBuilder.UseSqlServer(
"Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=MultipleOnesToSingleMany;Integrated Security=True");
using (var context = new TestContext(optionsBuilder.Options))
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
var entityB1 = new EntityB();
var entityB2 = new EntityB();
var entityB3 = new EntityB();
var entityB4 = new EntityB();
context.AddRange(new EntityA {EntityB = entityB2}, new EntityA {EntityB = entityB4});
context.AddRange(new EntityC { EntityB = entityB3 }, new EntityC { EntityB = entityB4 });
context.AddRange(entityB1, entityB2, entityB3, entityB4);
context.SaveChanges();
}
using (var context = new TestContext(optionsBuilder.Options))
{
var first = context.EntityAs
.Where(e => e.EntityB.EntityCs.Count() > 0)
.ToList();
var second = context.EntityCs
.Where(e => e.EntityB.EntityAs.Count() > 0)
.ToList();
var entityBToEntityA = context.EntityBs
.Where(e => e.EntityAs.Count() > 0)
.ToList();
var entityBToEntityC = context.EntityBs
.Where(e => e.EntityCs.Count() > 0)
.ToList();
var entityAToEntityB = context.EntityAs
.Where(e => e.EntityB.Id != Guid.Empty)
.ToList();
var entityCToEntityB = context.EntityCs
.Where(e => e.EntityB.Id != Guid.Empty)
.ToList();
var entityCToEntityBToEntityA = context.EntityCs
.Where(e => e.EntityB.EntityAs.Count() > 0)
.ToList();
var entityAToEntityBToEntityC = context.EntityAs
.Where(e => e.EntityB.EntityCs.Count() > 0)
.ToList();
}
var throwingOptionsBuilder = new DbContextOptionsBuilder();
throwingOptionsBuilder.UseSqlServer(
"Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=MultipleOnesToSingleMany;Integrated Security=True",
builder => builder.QueryClientEvaluationBehavior(QueryClientEvaluationBehavior.Throw));
using (var context = new TestContext(throwingOptionsBuilder.Options))
{
var first = context.EntityAs
.Where(e => e.EntityB.EntityCs.Count() > 0)
.ToList();
var second = context.EntityCs
.Where(e => e.EntityB.EntityAs.Count() > 0)
.ToList();
var entityBToEntityA = context.EntityBs
.Where(e => e.EntityAs.Count() > 0)
.ToList();
var entityBToEntityC = context.EntityBs
.Where(e => e.EntityCs.Count() > 0)
.ToList();
var entityAToEntityB = context.EntityAs
.Where(e => e.EntityB.Id != Guid.Empty)
.ToList();
var entityCToEntityB = context.EntityCs
.Where(e => e.EntityB.Id != Guid.Empty)
.ToList();
var entityCToEntityBToEntityA = context.EntityCs
.Where(e => e.EntityB.EntityAs.Count() > 0)
.ToList();
var entityAToEntityBToEntityC = context.EntityAs
.Where(e => e.EntityB.EntityCs.Count() > 0)
.ToList();
}
}
}
Adding tests to cover regressions in #5430