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

Code before:
        Assert.NotNull(original2.Error.InnerError);
        Assert.Equal("501", original2.Error.InnerError.Code);
        
        // Test case 3: Entity with null nested complex property (InnerError is null) - this was throwing InvalidCastException
        var job3 = new Job
        {
            Id = 3,
            Name = "Job with Error only",
            Error = new JobError
            {
                Code = "400",
                Message = "Bad Request"
            }
        };
        ctx.Jobs.Add(job3);
        ctx.SaveChanges();
        
        var original3 = ctx.Entry(job3).OriginalValues.ToObject() as Job;
        Assert.NotNull(original3);
        Assert.Equal("Job with Error only", original3.Name);
        Assert.NotNull(original3.Error);
        Assert.Equal("400", original3.Error.Code);
        Assert.Null(original3.Error.InnerError); // This was causing InvalidCastException before the fix
    }

    [ConditionalFact(Skip = "Complex collection with nested nullable complex properties not yet fully supported. Issue #31411")]
    public void OriginalValues_ToObject_with_complex_collection_containing_double_nested_nullable_complex_properties()
    {
        var modelBuilder = InMemoryTestHelpers.Instance.CreateConventionBuilder();
        modelBuilder.Entity<BlogWithTags>(eb =>
        {
            eb.ComplexCollection(e => e.Tags);
        });
        var model = modelBuilder.FinalizeModel();

        var serviceProvider = InMemoryTestHelpers.Instance.CreateContextServices(model);
        var stateManager = serviceProvider.GetRequiredService<IStateManager>();
        
        // Test with a complex collection where items have double nested nullable complex properties
        var blog = new BlogWithTags
        {
            Id = 1,
            Title = "Test Blog",
            Tags = new List<TagWithMetadata>
            {
                // Tag with null Metadata (complex property is null)
                new TagWithMetadata
                {
                    Name = "Tag1",
                    Metadata = null
                },

Review comment (suggestion):
@copilot Unskip this test and improve the fix to handle this scenario as well.
Try to explore other related scenarios and add more tests

Code after:
        Assert.Equal("Job with No Error", result.Name);
        Assert.Null(result.Error);
    }

    [ConditionalTheory]
    [ClassData(typeof(DataGenerator<bool?>))]
    public void ToObject_with_all_nested_complex_properties_populated(bool? useOriginalValues)
    {
        var job = new Job
        {
            Id = 1,
            Name = "Job with Error + Inner Errors",
            Error = new RootJobError
            {
                Code = "500",
                Message = "Internal Server Error",
                InnerError = new JobError
                {
                    Code = "501",
                    Message = "Not Implemented",
                    InnerError = new LeafJobError
                    {
                        Code = "502",
                        Message = "Bad Gateway"
                    }
                }
            }
        };

        var result = GetToObjectResult(job, useOriginalValues);

        Assert.Equal("Job with Error + Inner Errors", result.Name);
        Assert.NotNull(result.Error);
        Assert.Equal("500", result.Error!.Code);
        Assert.NotNull(result.Error.InnerError);
        Assert.Equal("501", result.Error.InnerError!.Code);
        Assert.NotNull(result.Error.InnerError.InnerError);
        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",