Skip to content

Owned property null despite having a SubOwned property with non-null data #25076

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
cjblomqvist opened this issue Jun 10, 2021 · 2 comments
Closed

Comments

@cjblomqvist
Copy link

cjblomqvist commented Jun 10, 2021

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.

public class Owner {
  public int Id { get; set; }
  public Owned Owned { get; set; }
}

public class Owned {
  public string NotRelevantEither { get; set; }
  public SubOwned SubOwned { get; set; }
}

public class SubOwned {
  public string Relevant { get; set; }
}

// In Context
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);
  });

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.

var owned = myContext.Owner.FirstOrDefault(o => o.Id == 1);
owned.Id == 1; // True
owned.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)

@smitpatel
Copy link
Contributor

This is essentially duplicate of #21488

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.

@cjblomqvist
Copy link
Author

Thanks!

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

3 participants