Skip to content

Unexpected behavior for non-optional complex type where all properties are nullable #37162

@dkokotov

Description

@dkokotov

Question

It is great that in .NET 10 support for optional/nullable complex properties was added. However, as part of this the release notes mention "Note that optional complex types currently require at least one required property to be defined on the complex type.". But it's unclear what the expected behavior is in that case, and if it's different depending on whether the property for the complex type is nullable or not.

In my scenario, I previously had a data model like this:

public class Project {
    public string Id { get; set; }
    public Lifetime Lifetime { get; set; 
}

public class Lifetime {
    public DateTime? Start { get; init; }

    public DateTime? End { get; init; }
}

Project.Lifetime was mapped as complex property. Note that the modeling is like this because all 4 combinations of Lifetime.Start and Lifetime.End nullability are possible.

In EF 9, if both were null, this was retrieved as as a not-null Lifetime with both Start and End null. But in .NET 10, this is retrieved as a null Lifetime - even though it is still declared as a not-null property. this is then breaking some downstream code.

I can deal with this (I guess by changing Project.Lifetime to be nullable), my question is whether it is expected behavior. I couldn't find documentation of Complex Type for EF 10 other than the single mention in the "what's new" above. And I tried to read the discussion in #31376 but I am still somewhat confused as to what is supported / expected in this case, and whether the observed behavior is what is expected or not.

It would be great if the documentation was updated to discuss this. Also any advice as to what I should do in the meantime would be very appreciated.

Your code

Stack traces


Verbose output


EF Core version

10.0.0

Database provider

Npgsql

Target framework

.NET 10

Operating system

Mac OS

IDE

Rider

Metadata

Metadata

Assignees

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions