Open gterdem opened 4 years ago
Note for triage: reproduces on latest daily, but I haven't dug into the model at all.
Query:
DbSet<VehicleBase>()
.AsNoTracking()
.Include("Drivers")
SQL
SELECT [v].[Id], [v].[Name], [v].[Type], [d].[Id], [d].[HasLicense], [d].[Title], [d].[VehicleId], [d0].[Id], [d0].[HasLicense], [d0].[Title], [d0].[VehicleId], [d1].[Id], [d1].[HasLicense], [d1].[Title], [d1].[VehicleId]
FROM [Vehicles] AS [v]
LEFT JOIN [Drivers] AS [d] ON [v].[Id] = [d].[VehicleId]
LEFT JOIN [Drivers] AS [d0] ON [v].[Id] = [d0].[VehicleId]
LEFT JOIN [Drivers] AS [d1] ON [v].[Id] = [d1].[VehicleId]
ORDER BY [v].[Id], [d].[Id], [d0].[Id], [d1].[Id]
Data repeats or just the tables in SQL? SQL will have repetition since they are 3 different navigations - 1 in each derived type.
@smitpatel
Related dotnet/efcore#20335
Issue here, the FK is shared across all derived type. So when generating joins, we don't account for discriminator, which generates extra rows in SQL result. When generating collections out of it in client side everything falls apart.
A proper SQL accounting for Discriminator would be
SELECT [v].[Id], [v].[Name], [v].[Type], [d].[Id], [d].[HasLicense], [d].[Title], [d].[VehicleId], [d0].[Id], [d0].[HasLicense], [d0].[Title], [d0].[VehicleId], [d1].[Id], [d1].[HasLicense], [d1].[Title], [d1].[VehicleId]
FROM [Vehicles] AS [v]
LEFT JOIN [Drivers] AS [d] ON [v].[Id] = [d].[VehicleId] AND [v].[Type] = 1
LEFT JOIN [Drivers] AS [d0] ON [v].[Id] = [d0].[VehicleId] AND [v].[Type] = 2
LEFT JOIN [Drivers] AS [d1] ON [v].[Id] = [d1].[VehicleId] AND [v].[Type] = 3
ORDER BY [v].[Id], [d].[Id], [d0].[Id], [d1].[Id]
Note from triage: we should look at whether it is possible/desirable to do anything automatically here, or rather if we should document including the discriminator in the FK manually for cases like this.
Note from triage: putting this in the 7.0 milestone to make sure we consider the design in 7.0. The outcome of that will determine together with prioritization against other work will determine whether or not this is implemented in 7.0.
Note from triage: doc this.
Including sub-collection causes repeated entity results, based on added sub-collection item.
Code below is psudo-kind, working sample link is here
When trying to query like
var vehiclesWithDrivers = await _context.Vehicles.AsNoTracking().Include("Drivers").ToListAsync();
result has repeating entities based on sub-collection members they have.DbContext
Steps to reproduce
I have created a sample project here at https://github.com/gterdem/EfCore-Inheritance.
Further technical details
EF Core version: 3.1.6 Database provider: Microsoft.EntityFrameworkCore.SqlServer and SqLite Target framework: .NET Core 3.1.301 Operating system: Windows 10