Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
DxsSucuk opened this issue Dec 14, 2023 · 4 comments · Fixed by #32662
Closed

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

DxsSucuk opened this issue Dec 14, 2023 · 4 comments · Fixed by #32662
Labels
area-model-building closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression type-bug
Milestone

Comments

@DxsSucuk
Copy link

DxsSucuk commented Dec 14, 2023

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

@DxsSucuk
Copy link
Author

DxsSucuk commented Dec 15, 2023

This Issue does not happen on older EF-Core versions.

@ajcvickers
Copy link
Member

Note for triage: Regression in the Unicodeness of the const string in the query is not being generated correctly when the compiled model is being used.

With the compiled mode--see the non-Unicode string literal:

SELECT CASE
    WHEN EXISTS (
        SELECT 1
        FROM [Tests] AS [t]
        WHERE [t].[Name] = 'RED') THEN CAST(1 AS bit)
    ELSE CAST(0 AS bit)
END

Without the compiled model--the literal is Unicode:

SELECT CASE
    WHEN EXISTS (
        SELECT 1
        FROM [Tests] AS [t]
        WHERE [t].[Name] = N'RED') THEN CAST(1 AS bit)
    ELSE CAST(0 AS bit)
END

In EF7, we generate this with and without the compiled model:

SELECT CASE
    WHEN EXISTS (
        SELECT 1
        FROM [Tests] AS [t]
        WHERE [t].[Name] = N'RED') THEN CAST(1 AS bit)
    ELSE CAST(0 AS bit)
END

@AndriySvyryd AndriySvyryd self-assigned this Dec 22, 2023
@AndriySvyryd AndriySvyryd added this to the 8.0.x milestone Dec 22, 2023
@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Dec 22, 2023
@AndriySvyryd AndriySvyryd removed their assignment Dec 27, 2023
@AndriySvyryd AndriySvyryd reopened this Jan 4, 2024
@DxsSucuk
Copy link
Author

Still does not work in 8.0.1

@roji
Copy link
Member

roji commented Jan 15, 2024

@DxsSucuk the fixed was merged for 8.0.2, which should be out in a few weeks.

@ajcvickers ajcvickers modified the milestones: 8.0.x, 8.0.2 Jan 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-model-building closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants