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

Code before:
        => TestHelpers.ExecuteWithStrategyInTransactionAsync(
            CreateContext,
            UseTransaction,
            async context =>
            {
                var query = await context.JsonEntitiesBasic.ToListAsync();
                var entity = query.Single();

                // Save original leaf value
                var originalLeaf = entity.OwnedReferenceRoot.OwnedReferenceBranch.OwnedReferenceLeaf;
                var originalLeafValue = originalLeaf.SomethingSomething;

                // Replace the owned reference with a new instance that shares nested reference navigations
                var oldRoot = entity.OwnedReferenceRoot;
                entity.OwnedReferenceRoot = new JsonOwnedRoot
                {
                    Name = "Modified",
                    Number = oldRoot.Number,
                    Names = oldRoot.Names,
                    Numbers = oldRoot.Numbers,
                    OwnedReferenceBranch = new JsonOwnedBranch
                    {
                        Id = oldRoot.OwnedReferenceBranch.Id,
                        Date = oldRoot.OwnedReferenceBranch.Date,
                        Enum = oldRoot.OwnedReferenceBranch.Enum,
                        Fraction = oldRoot.OwnedReferenceBranch.Fraction,
                        OwnedReferenceLeaf = originalLeaf,
                        OwnedCollectionLeaf = [],
                    },
                    OwnedCollectionBranch = [],
                };

                // Before DetectChanges, leaf should be accessible
                Assert.Same(originalLeaf, entity.OwnedReferenceRoot.OwnedReferenceBranch.OwnedReferenceLeaf);

                context.ChangeTracker.DetectChanges();

                // After DetectChanges, leaf should still be accessible
                Assert.NotNull(entity.OwnedReferenceRoot.OwnedReferenceBranch.OwnedReferenceLeaf);

                ClearLog();
                await context.SaveChangesAsync();

                // After SaveChanges, nested owned entities should still be accessible in memory
                Assert.NotNull(entity.OwnedReferenceRoot.OwnedReferenceBranch);
                Assert.NotNull(entity.OwnedReferenceRoot.OwnedReferenceBranch.OwnedReferenceLeaf);
                Assert.Equal(originalLeafValue, entity.OwnedReferenceRoot.OwnedReferenceBranch.OwnedReferenceLeaf.SomethingSomething);
            },
            async context =>
            {
                var result = await context.Set<JsonEntityBasic>().SingleAsync();

Review comment (suggestion):
The new `JsonOwnedBranch` initializer doesn’t copy `NullableEnum`, `Enums`, or `NullableEnums` from the existing branch, even though these are `init`-only properties in the model. That makes the test mutate additional JSON fields unrelated to the scenario, which can make the test more brittle and less targeted. Consider copying these values from `oldRoot.OwnedReferenceBranch` so the only intentional change is the root replacement while sharing the nested leaf instance.
```suggestion
                        Fraction = oldRoot.OwnedReferenceBranch.Fraction,
                        NullableEnum = oldRoot.OwnedReferenceBranch.NullableEnum,
                        Enums = oldRoot.OwnedReferenceBranch.Enums,
                        NullableEnums = oldRoot.OwnedReferenceBranch.NullableEnums,
```

Code after:
        => TestHelpers.ExecuteWithStrategyInTransactionAsync(
            CreateContext,
            UseTransaction,
            async context =>
            {
                var query = await context.JsonEntitiesBasic.ToListAsync();
                var entity = query.Single();

                // Save original leaf value
                var originalLeaf = entity.OwnedReferenceRoot.OwnedReferenceBranch.OwnedReferenceLeaf;
                var originalLeafValue = originalLeaf.SomethingSomething;

                // Replace the owned reference with a new instance that shares nested reference navigations
                var oldRoot = entity.OwnedReferenceRoot;
                entity.OwnedReferenceRoot = new JsonOwnedRoot
                {
                    Name = "Modified",
                    Number = oldRoot.Number,
                    Names = oldRoot.Names,
                    Numbers = oldRoot.Numbers,
                    OwnedReferenceBranch = new JsonOwnedBranch
                    {
                        Id = oldRoot.OwnedReferenceBranch.Id,
                        Date = oldRoot.OwnedReferenceBranch.Date,
                        Enum = oldRoot.OwnedReferenceBranch.Enum,
                        Fraction = oldRoot.OwnedReferenceBranch.Fraction,
                        NullableEnum = oldRoot.OwnedReferenceBranch.NullableEnum,
                        Enums = oldRoot.OwnedReferenceBranch.Enums,
                        NullableEnums = oldRoot.OwnedReferenceBranch.NullableEnums,
                        OwnedReferenceLeaf = originalLeaf,
                        OwnedCollectionLeaf = [],
                    },
                    OwnedCollectionBranch = [],
                };

                // Before DetectChanges, leaf should be accessible
                Assert.Same(originalLeaf, entity.OwnedReferenceRoot.OwnedReferenceBranch.OwnedReferenceLeaf);

                context.ChangeTracker.DetectChanges();

                // After DetectChanges, leaf should still be accessible
                Assert.NotNull(entity.OwnedReferenceRoot.OwnedReferenceBranch.OwnedReferenceLeaf);

                ClearLog();
                await context.SaveChangesAsync();

                // After SaveChanges, nested owned entities should still be accessible in memory
                Assert.NotNull(entity.OwnedReferenceRoot.OwnedReferenceBranch);
                Assert.NotNull(entity.OwnedReferenceRoot.OwnedReferenceBranch.OwnedReferenceLeaf);
                Assert.Equal(originalLeafValue, entity.OwnedReferenceRoot.OwnedReferenceBranch.OwnedReferenceLeaf.SomethingSomething);
            },