Skip to content

EF core 6 returns null value for owned entity #27516

@vsfeedback

Description

@vsfeedback

This issue has been moved from a ticket on Developer Community.


[severity:It's more difficult to complete my work] [regression] [worked-in:>Net Core 5, EF Core 5]
I recently upgraded my project to .Net Core 6, EF Core 6. I started getting this error message when I create a new migration or when I update-database:

Microsoft.EntityFrameworkCore.Model.Validation[20606]
The entity type 'Clinician.Name#PersonalName' is an optional dependent using table sharing without any required non shared property that could be used to identify whether the entity exists. If all nullable properties contain a null value in database then an object instance won't be created in the query. Add a required property to create instances with null values for other properties or mark the incoming navigation as required to always create an instance.

Ok, PersonalName is an owned entity consisting entirely of string properties. So I added a new property to the owned entity class:

public class PersonalName
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

// A non-nullable property to force owned entities to be created
    public bool NotUsed { get; set; }
}

That made the validation error go away, but now the property that should hold the owned entity is null, even though the SQL database has non-null values for many of the owned entity columns.

I have noticed a couple of (possibly) interesting things about this problem:

  1. If I remove the public bool NotUsed { get; set; } property then the query correctly returns the non-null owned entity.
  2. The new NotUsed Boolean column is nullable, despite the fact that it is a non-nullable data type and the parent entity does not inherit from a base entity class. Adding a [Required] attribute in the data model or fluent configuration in the DbContext has no effect on this.

Original Comments

Feedback Bot on 2/25/2022, 06:34 AM:

We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.


Original Solutions

(no solutions)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions