Skip to content

.Include with navigation collection with same name as other [NotMapped] property fails with InvalidOperationException #34242

Open
@DanielStout5

Description

@DanielStout5

File a bug

Given this class:

    public class Book : IBookCover2Haver
    {
        public List<BookCover> Covers { get; set; } = new();

        [NotMapped]
        List<BookCover2> IBookCover2Haver.Covers => Covers.Cast<BookCover2>().ToList();
    }

Calling this:

            var result = await db.Books
                .Include(x => x.Covers)
                .ToListAsync();

Throws this exception, even though the property being included is not marked with [NotMapped]:

InvalidOperationException: The expression 'x.Covers' is invalid inside an 'Include' operation, since it does not represent a property access: 't => t.MyProperty'. To target navigations declared on derived types, use casting ('t => ((Derived)t).MyProperty') or the 'as' operator ('t => (t as Derived).MyProperty'). Collection navigation access can be filtered by composing Where, OrderBy(Descending), ThenBy(Descending), Skip or Take operations. For more information on including related data, see http://go.microsoft.com/fwlink/?LinkID=746393.

Removing [NotMapped] from the second property does actually avoid this exception, but it seems to me that this is still a defect, since NotMapped should be specific to the property, not the name.

Include your code

Demo: https://github.com/DanielStout5/EfCoreBugs/tree/IncludeNotMapped

Include provider and version information

EF Core version: 7.0.11
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .Net 7

Metadata

Metadata

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions