Skip to content

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

Closed

Description

@roji

The following code:

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

await context.Database.ExecuteSqlAsync($"CREATE VIEW [BlogsView] AS SELECT [Id], [ComplexThing_Prop1], [ComplexThing_Prop2] FROM [Blogs];");

_ = 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.

Activity

  1. roji commented on Sep 18, 2024

    @roji
    MemberAuthor

    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.

  2. added this to the Backlog milestone on Sep 18, 2024
  3. added theissue type on Jun 15, 2025
  4. modified the milestones: Backlog, 11.0.0 on Dec 9, 2025
  5. added a commit that references this issue on Dec 14, 2025
    c29dd77
  6. roji commented on Dec 14, 2025

    @roji
    MemberAuthor

    This seems to be working fine; I've submitted #37364 to add a test covering the above querying (and SaveChanges) scenario, and we already have Update_complex_type_with_view_mapping and Update_complex_type_property_with_view_mapping for the ExecuteUpdate side.

  7. added 2 commits that reference this issue on Jan 5, 2026
    d7e7314
    fe23b47
  8. added a commit that references this issue on Jan 6, 2026
    2cc3da8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions