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

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

Open
DanielStout5 opened this issue Jul 17, 2024 · 1 comment

Comments

@DanielStout5
Copy link

DanielStout5 commented Jul 17, 2024

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

@cincuranet
Copy link
Contributor

Minimal self-contained repro:

using var db = new MyContext();
await db.Database.EnsureDeletedAsync();
await db.Database.EnsureCreatedAsync();
await db.Set<Book>()
    .Include(x => x.Covers)
    .LoadAsync();

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

    [NotMapped]
    List<BookCover2> IBookCover2Haver.Covers => Covers.Cast<BookCover2>().ToList();
}
public class BookCover
{
    public int Id { get; set; }
}
public class BookCover2 : BookCover
{ }

class MyContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        base.OnConfiguring(optionsBuilder);
        optionsBuilder.UseSqlite("Data Source=test.db");
        optionsBuilder.LogTo(Console.WriteLine);
        optionsBuilder.EnableSensitiveDataLogging();
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<Book>();
    }
}

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

4 participants