You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have not seen anything in other bugs related to this behaviour in current open issues. I will describe as best i can the behaviour. Here is the link of a small repo reproducing the issue : Test repo
protectedoverridevoidOnModelCreating(ModelBuilderbuilder){
builder.Entity<ApplicationUser>(entity =>{ entity.HasOne(p => p.Information).WithOne(i => i.User).HasForeignKey<UserInformation>(b => b.Id);});
builder.Entity<UserInformationPhoneNumber>().HasKey(u =>new{ u.PhoneNumberId, u.UserInformationId });// These are not really required as the relations are defined in the classes themselves// The behaviour does not change by adding these//builder.Entity<UserInformationPhoneNumber>().HasOne(s => s.UserInformation).WithMany(s => s.PhoneNumbers).HasForeignKey(s => s.UserInformationId);//builder.Entity<UserInformationPhoneNumber>().HasOne(s => s.PhoneNumber).WithMany().HasForeignKey(s => s.PhoneNumberId);}
public IActionResult Index(){if(!_context.ApplicationUsers.Any()){
AddUserToAppplication();}varquery= _context.ApplicationUsers
.Include(s => s.Information).ThenInclude(s => s.PhoneNumbers).ThenInclude(s => s.PhoneNumber)// Include generates empty objects with no Id or Values set.FirstOrDefault(s=> s.FirstName=="Name");return View();}
Expected behaviour
query returns a single "ApplicationUser" wich has within Information a single entry in the PhoneNumbers, wich contains a single PhoneNumber item containing the phone number.
Actual behaviour
PhoneNumber is an empty object with Id 0 and Value null (same is multiple PhoneNumber items), while the previous navigation property knows there's a ForeignKey for PhoneNumber with Id 1 that exists in the DB, the values are just not mapped when chained to the next .ThenInclude() .
** Sql generated **
SELECT [t].[Id], [t].[FirstName], [t].[LastName], [t].[Id0], [t0].[PhoneNumberId], [t0].[UserInformationId], [t0].[Id], [t0].[Value]
FROM (
SELECT TOP(1) [a].[Id], [a].[FirstName], [a].[LastName], [u].[Id] AS [Id0]
FROM [ApplicationUsers] AS [a]
LEFT JOIN [UserInformation] AS [u] ON [a].[Id] = [u].[Id]
WHERE [a].[FirstName] = N'Name'
) AS [t]
LEFT JOIN (
SELECT [u0].[PhoneNumberId], [u0].[UserInformationId], [p].[Id], [p].[Value]
FROM [UserInformationPhoneNumber] AS [u0]
INNER JOIN [PhoneNumber] AS [p] ON [u0].[PhoneNumberId] = [p].[Id]
) AS [t0] ON [t].[Id0] = [t0].[UserInformationId]
ORDER BY [t].[Id], [t].[Id0], [t0].[PhoneNumberId], [t0].[UserInformationId], [t0].[Id]
This query executed in SSMS returns :
Where we can see that the query itselfs returns the PhoneNumber.Value and PhoneNumber.Id
But while running we can see that PhoneNumber mapping was ignored once returned from SQL.
I hope that i haven't missed anything configuration wise, but this seemes like a pretty straightforward query that should work.
Provider and version information
EF Core version: 5.0.3
Visual Studio 16.8.6
Database provider: Microsoft.EntityFrameworkCore.SqlServer 2019
Target framework: NET 5.0 (core)
Operating system: Windows 10 20H2
IDE: (e.g. Visual Studio 2019 16.3)
The text was updated successfully, but these errors were encountered:
You are quite correct @anranruyye. Is this a design choice ? Should the mapping of the object returned from DB be ignored if there's a constructor for a new empty PhoneNumber ? This code worked on EF Core 2.1 and would map objects without a problem.
Introduction
I have not seen anything in other bugs related to this behaviour in current open issues. I will describe as best i can the behaviour. Here is the link of a small repo reproducing the issue : Test repo
Models
Relations definition
Adding mock data to query against
Bug part
Expected behaviour
query returns a single "ApplicationUser" wich has within Information a single entry in the PhoneNumbers, wich contains a single PhoneNumber item containing the phone number.
Actual behaviour
PhoneNumber is an empty object with Id 0 and Value null (same is multiple PhoneNumber items), while the previous navigation property knows there's a ForeignKey for PhoneNumber with Id 1 that exists in the DB, the values are just not mapped when chained to the next .ThenInclude() .
** Sql generated **
This query executed in SSMS returns :
Where we can see that the query itselfs returns the PhoneNumber.Value and PhoneNumber.Id
But while running we can see that PhoneNumber mapping was ignored once returned from SQL.
I hope that i haven't missed anything configuration wise, but this seemes like a pretty straightforward query that should work.
Provider and version information
EF Core version: 5.0.3
Visual Studio 16.8.6
Database provider: Microsoft.EntityFrameworkCore.SqlServer 2019
Target framework: NET 5.0 (core)
Operating system: Windows 10 20H2
IDE: (e.g. Visual Studio 2019 16.3)
The text was updated successfully, but these errors were encountered: