-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Diagnostics analyzer for unnecessary Include #28449
Comments
@enricobenedos The important point here is that: .ThenInclude(mrk => mrk.Marker)
.ThenInclude(mrkTp => mrkTp.Type) did not work in EF Core 5.0. It would fail to honor the last Include. The docs already document the correct way of writing this kind of Include, which is as you show in the "working code'.' |
Yes you are right, but why not include this change in the breaking changes list of Normally in production environments I think that developers will not use transition/not LTS versions as .NET 5.0, so migrating from Can it be convenient to specify from which version the breaking change is coming? |
What about 3? We had to skip .NET 5 because of Azure Functions, and that's where I'm running into this from. |
We just upgraded from .NET 5 to .NET 6 and EF Core 6. Now at runtime we run into the following exception:
The causing query works in .NET 5 but throws the shown exception in .NET 6: var order = await _RetailContext.WebShopOrder
.Include(x => x.WebShopOrderPosition)
.ThenInclude(x => x.Order)
.Include(x => x.WebShopOrderPosition)
.ThenInclude(x => x.WebShopOrderPositionBooking)
.ThenInclude(x => x.Sale)
.ThenInclude(x => x.Booking)
.ThenInclude(x => x.Item)
.Include(x => x.WebShopOrderPosition)
.ThenInclude(x => x.WebShopOrderPositionBooking)
.ThenInclude(x => x.Sale)
.ThenInclude(x => x.Booking)
.ThenInclude(x => x.Season)
.Where(o => o.OrderId == request.OrderId) The exception tells me that the navigation was ignored from include because some fix-up automatically populates it. Why is a fix-up ignoring all includes afterwards? The exception tells us we can suppress the exception, but that will not fix the problem that includes are ignored which are required. |
I configured our DbContext to ignore that exception and checked if all required navigations are loaded. So I am not sure if I misunderstand the exception message or if the exception message is misleading, but everything seems fine. protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.ConfigureWarnings(builder =>
{
builder.Ignore(CoreEventId.NavigationBaseIncludeIgnored);
});
} |
@JanKotschenreuther If you are ignoring the warning and getting the same behavior, then that confirms that you have unneeded Incudes. Specifically, the code to include the relationship between _RetailContext.WebShopOrder
.Include(x => x.WebShopOrderPosition) Does not then need to be also specified in the other direction with: .ThenInclude(x => x.Order) |
Thank you for that hint @ajcvickers . I did not notice that the Order navigation on WebShopOrderPosition is also a WebShopOrder entity. Is there an analyzer which helps avoiding unnecessary includes? |
Note from triage: putting on the backlog (and adding to #22086) to consider an analyzer, although this is a difficult problem to solve since it requires understanding of the EF model at compile time. |
This definitely should have been mentioned in breaking changes from |
Good morning,
after upgrading Entity Framework Core to .NET 6 I faced a breaking change on my production API that was not listed on these docs.
It is related to
Microsoft.EntityFrameworkCore.Query.NavigationBaseIncludeIgnored
that change the EF Core behaviour when using multiple level properties.The solution is explained on this question on Stack Overflow.
Old working code:
New working code:
Can it be a good idea to include it in the docs?
Thank you
The text was updated successfully, but these errors were encountered: