Skip to content

String comparison fails when using const and compiled Models. #32617

@DxsSucuk

Description

@DxsSucuk

File a bug

Description

When comparing an entity value with a const field value while also using compiled Models, EF Core creates a different SQL Statement and causes the query to fail. When you instead use a static field, it creates the correct SQL Statement and the query succeeds.

Include your code

Here is a simple snippet with comments explaining the Issue.

    private static string name = "RĒD";
    private const string name2 = "RĒD";
    public static void Main(string[] args)
    {
        using (var context = new ApplicationContextDB())
        {
            // Use the working static field.
            if (!context.Tests.Any(c => c.Name == name))
            {
                // Using the const field as value, doesn't make a difference which one is being used to set.
                context.Add(new Test { Name = name2 });
                context.SaveChanges();
            }

            // The static test should work always.
            Console.WriteLine($"Static Test: {context.Tests.Any(c => c.Name == name)}");

            // The const test should fail when using Compiled Models, because the SQL statement build by EF Core is different.
            Console.WriteLine($"Const Test: {context.Tests.Any(c => c.Name == name2)}");

            // A workaround for this Issue is to outcommend the useModel call inside the ApplicationContextDB class.
        }
    }

I will be including a zip files containing code that can be used to reproduce it.
EFCoreTest.zip

Include stack traces

No stacktrace.

Include verbose output

No related to the Package Manager or CLI.

Include provider and version information

EF Core version: 8.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 8.0
Operating system: Windows 11
IDE: Visual Studio 2022 17.8.3

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions