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
See below code for the setup and configuration (sorry about not including a full repro repo). In the DB (MSSQL), it'd all map to one table Owner.
publicclassOwner{publicintId{get;set;}publicOwnedOwned{get;set;}}publicclassOwned{publicstringNotRelevantEither{get;set;}publicSubOwnedSubOwned{get;set;}}publicclassSubOwned{publicstringRelevant{get;set;}}// In ContextmodelBuilder.Entity<Owner>()// Needs to be placed below above bca.BomColor instruction/command (for unknown reasons).OwnsOne(owner =>owner.Owned, oo =>{oo.OwnsOne(owned =>owned.SubOwned);});
Let's assume the following data in the DB for the Owner table:
Id = 1
Owned_NotRelevantEither = NULL
Owned_SubOwned_Relevant = "Something Relevant"
The issue:
When I query for Owner then Owned is null (and thus I do not get the SubOwned data back), e.g.
varowned=myContext.Owner.FirstOrDefault(o =>o.Id==1);owned.Id==1;// Trueowned.Owned==null;// True -- This is wrong, it should be set with SubOwned set to an object with Relevant = "Something Revelant"
It doesn't matter if I do myContext.Owner.Include(o => o.Owned).ThenInclude(o => o.SubOwned)...
It does matter if I set Owned as required, e.g.
modelBuilder.Entity<Owner>()// Needs to be placed below above bca.BomColor instruction/command (for unknown reasons).OwnsOne(owner =>owner.Owned, oo =>{oo.OwnsOne(owned =>owned.SubOwned);}).Navigation(o =>o.Owned).IsRequired();
Am I doing something wrong or is this a bug?
Include provider and version information
EF Core version:
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (e.g. .NET 5.0)
Operating system:
IDE: (e.g. Visual Studio 2019 16.9.3)
The text was updated successfully, but these errors were encountered:
A very detailed analysis of the issue is in #23564
TLDR - When materializing object of type Owned we see that it doesn't have any non-PK columns with value in it, hence we create a null object for it essentially losing any nested owned objects like SubOwned. The issue is we don't have a way to know if Owned exist or not by looking at only columns of Owned. If you add a non-nullable property to Owned (type/value doesn't matter, could be just bool property too), then we can use that column to see if instance of Owned exist vs it is null and we will materialize results correctly.
See below code for the setup and configuration (sorry about not including a full repro repo). In the DB (MSSQL), it'd all map to one table Owner.
Let's assume the following data in the DB for the Owner table:
Id = 1
Owned_NotRelevantEither = NULL
Owned_SubOwned_Relevant = "Something Relevant"
The issue:
When I query for Owner then Owned is null (and thus I do not get the SubOwned data back), e.g.
It doesn't matter if I do myContext.Owner.Include(o => o.Owned).ThenInclude(o => o.SubOwned)...
It does matter if I set Owned as required, e.g.
Am I doing something wrong or is this a bug?
Include provider and version information
EF Core version:
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (e.g. .NET 5.0)
Operating system:
IDE: (e.g. Visual Studio 2019 16.9.3)
The text was updated successfully, but these errors were encountered: