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

Error querying on complex type whose container is mapped to a table and a view #34706

Open
roji opened this issue Sep 18, 2024 · 1 comment
Open

Comments

@roji
Copy link
Member

roji commented Sep 18, 2024

The following code:

await using var context = new BlogContext();
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();

_ = await context.Blogs.Where(b => b.ComplexThing.Prop1 == 8).ToListAsync();

public class BlogContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseSqlServer("Server=localhost;Database=test;Connect Timeout=60;ConnectRetryCount=0;Encrypt=false")
            .LogTo(Console.WriteLine, LogLevel.Information)
            .EnableSensitiveDataLogging();

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Blog>()
            .ToTable("Blogs")
            .ToView("BlogsView")
            .ComplexProperty(b => b.ComplexThing);
    }
}

public class Blog
{
    public int Id { get; set; }
    public ComplexThing ComplexThing { get; set; }
}

public class ComplexThing
{
    public int Prop1 { get; set; }
    public int Prop2 { get; set; }
}

... throws:

Unhandled exception. System.Collections.Generic.KeyNotFoundException: The given key 'DefaultTable: Blog' was not present in the dictionary.
   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at Microsoft.EntityFrameworkCore.Query.SqlExpressions.SelectExpression.GenerateComplexPropertyShaperExpression(StructuralTypeProjectionExpression containerProjection, IComplexProperty complexProperty) in /Users/roji/projects/efcore/src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs:line 2672
   at Microsoft.EntityFrameworkCore.Query.StructuralTypeProjectionExpression.BindComplexProperty(IComplexProperty complexProperty) in /Users/roji/projects/efcore/src/EFCore.Relational/Query/StructuralTypeProjectionExpression.cs:line 375
   at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.BindComplexProperty(StructuralTypeReferenceExpression typeReference, IComplexProperty complexProperty) in /Users/roji/projects/efcore/src/EFCore.Relational/Query/RelationalSqlTranslatingExpressionVisitor.cs:line 1331
   at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.TryBindMember(Expression source, MemberIdentity member, Expression& expression, IPropertyBase& property) in /Users/roji/projects/efcore/src/EFCore.Relational/Query/RelationalSqlTranslatingExpressionVisitor.cs:line 1208

This is because calling GetViewOrTableMappings() on the complex type returns only the table mapping - this seems like a metadata issue.

Once this fixed, GenerateComplexPropertyShaperExpression would need to be fixed to support this scenario as well.

@roji
Copy link
Member Author

roji commented Sep 18, 2024

The metadata support is already covered by #34627. Once that's done, this issue tracks doing the proper adjustments to GenerateComplexPropertyShaperExpression.

Adjustments will also be needed for property handling of ExecuteUpdate when a complex type is referenced as the property to change, and there's a view mapping; see test Update_complex_type_type_with_view_mapping.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants