Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Owned Entity throws InvalidOperationException inside RelationalSqlTranslatingExpressionVisitor.TryRewriteEntityEquality #29142

Closed
andygjp opened this issue Sep 19, 2022 · 2 comments
Assignees
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression Servicing-approved type-bug
Milestone

Comments

@andygjp
Copy link

andygjp commented Sep 19, 2022

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

@ajcvickers
Copy link
Member

Note for triage: still fails with latest daily build.

@smitpatel
Copy link
Member

Same fix as #28247 but in entity equality rather than materialization.

@smitpatel smitpatel self-assigned this Sep 20, 2022
@ajcvickers ajcvickers added this to the 7.0.0 milestone Sep 21, 2022
@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Sep 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression Servicing-approved type-bug
Projects
None yet
Development

No branches or pull requests

3 participants