efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Owned Entity throws InvalidOperationException inside RelationalSqlTranslatingExpressionVisitor.TryRewriteEntityEquality

Open andygjp opened this issue 3 years ago • 2 comments
trafficstars

File a bug

A query that ran in v6.0.9 now fails with RC1.

Code sample

I've recreated the issue in a repo: https://github.com/andygjp/OwnedTypesError.

If I have the following model:

internal class Context : DbContext
{
    public Context(DbContextOptions<Context> options) : base(options)
    {
    }

    public DbSet<Entity> Entities { get; set; }
    
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Entity>().HasKey(x => x.EntityID);
        modelBuilder.Entity<Entity>().OwnsOne(x => x.OwnedOne);
    }
}

internal class Entity
{
    public int EntityID { get; set; }
    public string? Value { get; set; }
    public OwnedOne? OwnedOne { get; set; }
}

internal class OwnedOne
{
    public string? Value { get; set; }
}

I get an exception if I have a second where clause and I use the RC1 version of EF Core.

new Context(dbContextOptions).Entities
    .AsNoTracking()
     // 1st
    .Where(x => x.EntityID == existingEntity.EntityID)
    .Select(x => x.OwnedOne)
    // 2nd
    .Where(x => x != null)
    .ToList();

Version 6.0.9 does not raise any exceptions.

You can run the program in the repo as is and it will complete without error. Then edit the package references in the csproj and run it again and it will throw.

Stack traces

Unhandled exception. System.InvalidOperationException: Sequence contains no elements
at System.Linq.ThrowHelper.ThrowNoElementsException()
at System.Linq.Enumerable.Aggregate[TSource](IEnumerable`1 source, Func`3 func)
at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.TryRewriteEntityEquality(ExpressionType nodeType, Expression left, Expression right, Boolean equalsMethod, Expression& result)
at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.VisitBinary(BinaryExpression binaryExpression)
at Microsoft.EntityFrameworkCore.SqlServer.Query.Internal.SqlServerSqlTranslatingExpressionVisitor.VisitBinary(BinaryExpression binaryExpression)
at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.TranslateInternal(Expression expression)
at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.Translate(Expression expression)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateExpression(Expression expression)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateLambdaExpression(ShapedQueryExpression shapedQueryExpression, LambdaExpression lambdaExpression)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateWhere(ShapedQueryExpression source, LambdaExpression predicate)
at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass9_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Program.<<Main>$>g__GetEntity|0_7(DbContextOptions`1 dbContextOptions, Entity existingEntity, Expression`1 predicate)
at Program.<Main>$(String[] args)

Versions

EF Core version: 7.0.0-rc.1.22426.7 Database provider: Microsoft.EntityFrameworkCore.Sqlite v7.0.0-rc.1.22426.7 (or Microsoft.EntityFrameworkCore.SqlServer) Target framework: .NET 7.0 Operating system: MacOS

andygjp avatar Sep 19 '22 12:09 andygjp

Note for triage: still fails with latest daily build.

ajcvickers avatar Sep 19 '22 13:09 ajcvickers

Same fix as https://github.com/dotnet/efcore/issues/28247 but in entity equality rather than materialization.

smitpatel avatar Sep 19 '22 17:09 smitpatel