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
[Regression] ThenInclude back to parent entity isn't working #23674
Labels
area-query
closed-fixed
The issue has been fixed and is/will be included in the release indicated by the issue milestone.
customer-reported
regression
Servicing-approved
type-bug
Milestone
Comments
Simplified repro: using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
namespace EfCore5Test
{
public sealed class Question
{
public Guid Id { get; set; }
public IList<QuestionLocalization> Localizations { get; set; }
public IList<Answer> Answers { get; set; }
}
public sealed class QuestionLocalization
{
public Guid Id { get; set; }
public Guid QuestionId { get; set; }
public Question Question { get; set; }
}
public sealed class Answer
{
public Guid Id { get; set; }
public Guid QuestionId { get; set; }
public Question Question { get; set; }
}
public sealed class ApplicationDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=aspnet-WebApplication-6E483A89-BEE6-4A76-96CC-CEB276E5E112;Trusted_Connection=True;MultipleActiveResultSets=true");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<QuestionLocalization>(builder =>
{
builder.HasOne(x => x.Question).WithMany(x => x.Localizations).HasForeignKey(x => x.QuestionId).OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity<Answer>(builder =>
{
builder.HasOne(x => x.Question).WithMany(x => x.Answers).HasForeignKey(x => x.QuestionId).OnDelete(DeleteBehavior.Cascade);
});
}
public DbSet<Question> Questions { get; set; }
public DbSet<QuestionLocalization> QuestionLocalizations { get; set; }
public DbSet<Answer> Answers { get; set; }
}
class Program
{
static void Main(string[] args)
{
using (var context = new ApplicationDbContext())
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
var query = context.Questions
.Include(x => x.Localizations)
.ThenInclude(x => x.Question.Answers);
Console.WriteLine(query.ToQueryString());
}
}
}
} |
maumar
added a commit
that referenced
this issue
Dec 15, 2020
maumar
added a commit
that referenced
this issue
Dec 15, 2020
maumar
added a commit
that referenced
this issue
Dec 15, 2020
There are 2 sub-issues involved in this
|
Design meeting decision
|
@smitpatel Don't forget about this one. :-) |
First thing on my list to fix once I get my home office setup back. |
smitpatel
added
the
closed-fixed
The issue has been fixed and is/will be included in the release indicated by the issue milestone.
label
Feb 22, 2021
smitpatel
added a commit
that referenced
this issue
Feb 22, 2021
given 5.0.4 is released , should this be closed? |
This has been merged to respective branches according to resolution above. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
area-query
closed-fixed
The issue has been fixed and is/will be included in the release indicated by the issue milestone.
customer-reported
regression
Servicing-approved
type-bug
Steps to reproduce
Notice that there is no join with the
Answers
table, so when the query is executed,Question.Answers
is not included in the result (you can see that if you put some data into the tables first and then execute the query) with no warning or error generated.In EF Core 3.1, this worked correctly and
Answers
was indeed included. I agree that the 3.1 generated query looks a bit complicated and is probably suboptimal and that this way to include "backwards" to the parent and then to another entity seems odd to me but I found it in our code base and noticed it stopped working (I'm guessing the author did this to avoid repeating.Include(x => x.Questions)
- in the real code, there's actually one moreInclude
before that).I know that the proper way would have been to do this all along:
which still works in EF Core 5.0 and it generates better SQL, so I'm going to rewrite the code to this. But the original piece of code just silently stopped working correctly. If it was intentional to make this a breaking change, it should have been documented and also it should throw an error or a warning instead of just running an incomplete query and returning incomplete data 😕
Include provider and version information
EF Core version: 5.0.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 5.0
Operating system: Windows 10 Version 1909
IDE: Visual Studio 16.9.0 Preview 2.0
The text was updated successfully, but these errors were encountered: