dotnet / ef6

This is the codebase for Entity Framework 6 (previously maintained at https://entityframework.codeplex.com). Entity Framework Core is maintained at https://github.com/dotnet/efcore.
https://docs.microsoft.com/ef/ef6
MIT License
1.43k stars 545 forks source link

Entity Framework can't load related entities when use Query().Load() for many to many relation #589

Closed NikolayYeryomenko closed 6 years ago

NikolayYeryomenko commented 6 years ago

I have entity Page with has many HtmlBlocks and entity HtmlBlock with belongs to many pages (has collection of pages).

I configured many to many relation:

    modelBuilder.Entity<Core.Page>()
                .HasMany(p => p.HtmlBlocks)
                .WithMany(hb => hb.Pages)
                .Map(m =>
                {
                    m.ToTable("PageHtmlBlocks");
                    m.MapLeftKey("PageId");
                    m.MapRightKey("HtmlBlockId");
                });

When I execute:

Core.Page page = _db.Pages.Where(p => p.PageId == pageId).FirstOrDefault();
_db.Entry(page).Collection(p => p.HtmlBlocks)                
                .Load();

this code loads HtmlBlocks related to page.

When I execute:

Core.Page page = _db.Pages.Where(p => p.PageId == pageId).FirstOrDefault();
_db.Entry(page).Collection(p => p.HtmlBlocks)                
        .Query()
                .Load();

this code doesn't load related entities, but generates the same and correct SQL query to fetch data.

I use Entity Framework 6.2 I think this is a bug, please fix this in the future releases.

EF version: 6.2 Database Provider: EntityFramework.SqlServer 6.0.0.0 Operating system: Windows 10 Pro 1803 IDE: Visual Studio 2017 15.6

ajcvickers commented 6 years ago

EF Team Triage: This is a known limitation of the lower-level implementation of this method and would require significant work to overcome. This issue is not something that our team is planning to address in the EF6.x code base. This does not mean that we would not consider a community contribution to address this issue.

Moving forwards, our team will be fixing bugs, implementing small improvements, and accepting community contributions to the EF6.x code base. Larger feature work and innovation will happen in the EF Core code base (https://github.com/aspnet/EntityFramework).

Closing an issue in the EF6.x project does not exclude us addressing it in EF Core. In fact, a number of popular feature requests for EF have already been implemented in EF Core (alternate keys, batching in SaveChanges, etc.).

BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we'd like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.