Repository: dotnet/efcore
File: test/EFCore.SqlServer.FunctionalTests/ComplexTypesTrackingSqlServerTest.cs (C#)

Code before:
﻿// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.EntityFrameworkCore;

public class ComplexTypesTrackingSqlServerTest(
    ComplexTypesTrackingSqlServerTest.SqlServerFixture fixture,
    ITestOutputHelper testOutputHelper)
    : ComplexTypesTrackingSqlServerTestBase<ComplexTypesTrackingSqlServerTest.SqlServerFixture>(fixture, testOutputHelper)
{
    [ConditionalFact]
    public virtual async Task Can_read_original_values_with_TPH_shared_complex_property_column_null()
    {
        await using var context = CreateContext();

        await context.Database.CreateExecutionStrategy().ExecuteAsync(
            context,
            async context =>
            {
                await using var transaction = await context.Database.BeginTransactionAsync();

                context.Add(new InheritedItem1 { Name = "Item1" });
                await context.SaveChangesAsync();

                context.ChangeTracker.Clear();
                var item = await context.Set<InheritedItemBase>().FirstAsync();
                var entry = context.ChangeTracker.Entries().First();
                var originalItem = (InheritedItemBase)entry.OriginalValues.ToObject();

                Assert.NotNull(originalItem);
                Assert.Equal("Item1", originalItem.Name);
            });
    }

    [ConditionalFact]
    public virtual async Task Can_read_original_values_with_TPH_shared_complex_property_column_with_value()
    {
        await using var context = CreateContext();

        await context.Database.CreateExecutionStrategy().ExecuteAsync(
            context,
            async context =>
            {
                await using var transaction = await context.Database.BeginTransactionAsync();

                context.Add(new InheritedItem1 { Name = "Item1", SharedPrice = new SharedPrice { Amount = "10.99" } });
                await context.SaveChangesAsync();

                context.ChangeTracker.Clear();
                var item = await context.Set<InheritedItem1>().FirstAsync();
                var entry = context.ChangeTracker.Entries().First();

Review comment (bug):
@copilot This fails with the following error. Make sure to actually run this test after the fix. Note that the original issue was reported at https://github.com/dotnet/efcore/issues/37335
```
System.InvalidOperationException : An error was generated for warning 'Microsoft.EntityFrameworkCore.Query.FirstWithoutOrderByAndFilterWarning': The query uses the 'First'/'FirstOrDefault' operator without 'OrderBy' and filter operators. This may lead to unpredictable results. This exception can be suppressed or logged by passing event ID 'CoreEventId.FirstWithoutOrderByAndFilterWarning' to the 'ConfigureWarnings' method in 'DbContext.OnConfiguring' or 'AddDbContext'.
```

Code after:
﻿// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.EntityFrameworkCore;

public class ComplexTypesTrackingSqlServerTest(
    ComplexTypesTrackingSqlServerTest.SqlServerFixture fixture,
    ITestOutputHelper testOutputHelper)
    : ComplexTypesTrackingSqlServerTestBase<ComplexTypesTrackingSqlServerTest.SqlServerFixture>(fixture, testOutputHelper)
{
    [ConditionalFact]
    public virtual async Task Can_read_original_values_with_TPH_shared_complex_property_column_null()
    {
        await using var context = CreateContext();

        await context.Database.CreateExecutionStrategy().ExecuteAsync(
            context,
            async context =>
            {
                await using var transaction = await context.Database.BeginTransactionAsync();

                context.Add(new InheritedItem1 { Name = "Item1" });
                await context.SaveChangesAsync();

                context.ChangeTracker.Clear();
                var item = await context.Set<InheritedItemBase>().Where(i => i.Name == "Item1").FirstAsync();
                var entry = context.ChangeTracker.Entries().First();
                var originalItem = (InheritedItemBase)entry.OriginalValues.ToObject();

                Assert.NotNull(originalItem);
                Assert.Equal("Item1", originalItem.Name);
            });
    }

    [ConditionalFact]
    public virtual async Task Can_read_original_values_with_TPH_shared_complex_property_column_with_value()
    {
        await using var context = CreateContext();

        await context.Database.CreateExecutionStrategy().ExecuteAsync(
            context,
            async context =>
            {
                await using var transaction = await context.Database.BeginTransactionAsync();

                context.Add(new InheritedItem1 { Name = "Item1", SharedPrice = new SharedPrice { Amount = "10.99" } });
                await context.SaveChangesAsync();

                context.ChangeTracker.Clear();
                var item = await context.Set<InheritedItem1>().Where(i => i.Name == "Item1").FirstAsync();
                var entry = context.ChangeTracker.Entries().First();