Repository: dotnet/efcore
File: test/EFCore.Tests/ChangeTracking/Internal/PropertyValuesTest.cs (C#)

Code before:
            {
                b.ComplexProperty(x => x.Error);
            });
        }
    }

#nullable enable
    private class Job
    {
        public int Id { get; set; }
        public string Name { get; set; } = null!;
        public JobError? Error { get; set; }
    }

    private class JobError
    {
        public string Code { get; set; } = null!;
        public string Message { get; set; } = null!;
        public JobError? InnerError { get; set; }
    }
#nullable disable

    [ConditionalFact]
    public void Should_not_throw_error_when_property_do_not_exist()
    {
        // Arrange
        const string NameValue = "Simple Name";
        const string NewNameValue = "A New Name";

        using var ctx = new CurrentValuesDb();
        var entity = ctx.SimpleEntities.Add(new SimpleEntity { Name = NameValue });
        ctx.SaveChanges();

        entity.Entity.Name = NewNameValue;

        // Act
        var current = entity.CurrentValues.TryGetValue<string>("Non_Existent_Property", out var non_existent_current);
        var original = entity.OriginalValues.TryGetValue<string>("Non_Existent_Property", out var non_existent_original);

        // Assert
        Assert.False(current);
        Assert.False(original);

        Assert.Null(non_existent_current);
        Assert.Null(non_existent_original);
    }

    private class CurrentValuesDb : DbContext
    {
        // ReSharper disable once UnusedAutoPropertyAccessor.Local
        public DbSet<SimpleEntity> SimpleEntities { get; set; }

Review comment (suggestion):
@copilot Add another test with a complex collection containing double nested nullable complex properties and use a object with a non-empty complex collection where one of the items has a null complex property and the other has a nested null complex property.

Code after:
        Assert.Equal("502", result.Error.InnerError.InnerError!.Code);
    }

    [ConditionalTheory]
    [ClassData(typeof(DataGenerator<bool?>))]
    public void ToObject_with_null_nested_complex_property(bool? useOriginalValues)
    {
        var job = new Job
        {
            Id = 1,
            Name = "Job with Error only",
            Error = new RootJobError
            {
                Code = "400",
                Message = "Bad Request"
            }
        };

        var result = GetToObjectResult(job, useOriginalValues);

        Assert.Equal("Job with Error only", result.Name);
        Assert.NotNull(result.Error);
        Assert.Equal("400", result.Error!.Code);
        Assert.Null(result.Error.InnerError);
    }

    [ConditionalTheory]
    [ClassData(typeof(DataGenerator<bool?>))]
    public void ToObject_with_complex_collection_containing_nested_nullable_complex_properties(
        bool? useOriginalValues)
    {
        var job = new Job
        {
            Id = 1,
            Name = "Test Job",
            Errors =
            [
                new RootJobError { Code = "400", Message = "Bad Request", InnerError = null },
                new RootJobError
                {
                    Code = "500",
                    Message = "Server Error",
                    InnerError = new JobError { Code = "501", Message = "Not Implemented", InnerError = null }
                },
                new RootJobError
                {
                    Code = "503",
                    Message = "Service Unavailable",
                    InnerError = new JobError
                    {
                        Code = "504",