dotnet / dotnet/efcore

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

Open
#34,242 1 comment 0 reactions 0 assignees View on GitHub
area-query customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

## File a bug

Given this class:
```
public class Book : IBookCover2Haver
{
public List Covers { get; set; } = new();

[NotMapped]
List IBookCover2Haver.Covers => Covers.Cast().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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.