Repository: dotnet/efcore
File: test/EFCore.Specification.Tests/PropertyValuesTestBase.cs (C#)

Code before:
    }

    [ConditionalFact]
    public virtual void Nullable_complex_property_with_null_value_returns_null_when_using_ToObject()
    {
        using var context = CreateContext();
        var building = context.Set<Building>().Single(b => b.Name == "Building One");

        Assert.NotNull(building.OptionalMilk);
        building.OptionalMilk = null;

        var currentBuilding = (Building)context.Entry(building).CurrentValues.ToObject();
        Assert.Null(currentBuilding.OptionalMilk);

        var originalBuilding = (Building)context.Entry(building).OriginalValues.ToObject();
        Assert.NotNull(originalBuilding.OptionalMilk);
    }

    [ConditionalFact]
    public virtual async Task Reloading_optional_complex_property_with_null_does_not_throw()
    {
        using var context = CreateContext();
        var building = Building.Create(Guid.NewGuid(), "Building Null", 123m);
        building.OptionalMilk = null;
        context.Add(building);
        await context.SaveChangesAsync();

        await context.Entry(building).ReloadAsync();

        Assert.Null(building.OptionalMilk);
        Assert.Equal(EntityState.Unchanged, context.Entry(building).State);
    }

    [ConditionalFact]
    public virtual void Setting_current_values_from_cloned_values_sets_nullable_complex_property_to_null()
    {
        using var context = CreateContext();
        var building = context.Set<Building>().Single(b => b.Name == "Building One");

        Assert.NotNull(building.OptionalMilk);
        building.OptionalMilk = null;

        var clonedValues = context.Entry(building).CurrentValues.Clone();
        Assert.Null(((Building)clonedValues.ToObject()).OptionalMilk);

        building.OptionalMilk = new Milk
        {
            License = new License { Charge = 1.0m, Tag = new Tag { Text = "Ta1" }, Title = "Ti1", Tog = new Tog { Text = "To1" } },
            Manufacturer = new Manufacturer { Name = "M1", Rating = 7, Tag = new Tag { Text = "Ta2" }, Tog = new Tog { Text = "To2" } },
            Rating = 8,
            Species = "S1",

Review comment (suggestion):
Don't call `SaveChangesAsync`, instead modify SeedAsync to make `OptionalMilk` null on "Building Two"

Code after:
    }

    [ConditionalFact]
    public virtual void Nullable_complex_property_with_null_value_returns_null_when_using_ToObject()
    {
        using var context = CreateContext();
        var building = context.Set<Building>().Single(b => b.Name == "Building One");

        Assert.NotNull(building.OptionalMilk);
        building.OptionalMilk = null;

        var currentBuilding = (Building)context.Entry(building).CurrentValues.ToObject();
        Assert.Null(currentBuilding.OptionalMilk);

        var originalBuilding = (Building)context.Entry(building).OriginalValues.ToObject();
        Assert.NotNull(originalBuilding.OptionalMilk);
    }

    [ConditionalFact]
    public virtual async Task Reloading_optional_complex_property_with_null_does_not_throw()
    {
        using var context = CreateContext();
        var building = context.Set<Building>().Single(b => b.Name == "Building Two");
        Assert.Null(building.OptionalMilk);

        await context.Entry(building).ReloadAsync();

        Assert.Null(building.OptionalMilk);
        Assert.Equal(EntityState.Unchanged, context.Entry(building).State);
    }

    [ConditionalFact]
    public virtual void Setting_current_values_from_cloned_values_sets_nullable_complex_property_to_null()
    {
        using var context = CreateContext();
        var building = context.Set<Building>().Single(b => b.Name == "Building One");

        Assert.NotNull(building.OptionalMilk);
        building.OptionalMilk = null;

        var clonedValues = context.Entry(building).CurrentValues.Clone();
        Assert.Null(((Building)clonedValues.ToObject()).OptionalMilk);

        building.OptionalMilk = new Milk
        {
            License = new License { Charge = 1.0m, Tag = new Tag { Text = "Ta1" }, Title = "Ti1", Tog = new Tog { Text = "To1" } },
            Manufacturer = new Manufacturer { Name = "M1", Rating = 7, Tag = new Tag { Text = "Ta2" }, Tog = new Tog { Text = "To2" } },
            Rating = 8,
            Species = "S1",
            Validation = false
        };