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

Achieving database null semantics with string concatenation #33616

Open
slepmog opened this issue Apr 26, 2024 · 0 comments
Open

Achieving database null semantics with string concatenation #33616

slepmog opened this issue Apr 26, 2024 · 0 comments

Comments

@slepmog
Copy link

slepmog commented Apr 26, 2024

Some time ago (issue #3836) it was decided that the null semantic of string concatenation is wrong and should be replaced with C# semantic.
From then on, when EF detects a string concatenation in your LINQ, it translates each concatenated argument into SQL by wrapping it into COALESCE:

... = t.Field1 + t.Field2 + t.Field3, ...

becomes

... COALESCE([t].[Field1], N'') + COALESCE([t].[Field2], N'') + COALESCE([t].[Field3], N''), ...

This behaviour may be surprisingly annoying and disruptive, resulting in having to write very convoluted code with superfluous null checks that is then translated into an SQL monstrosity that runs slow because it has to evaluate the arguments twice (once for the null comparison, then for the concatenation if the comparison holds). All where the raw SQL version would neatly eliminate the unwanted nulls automatically.

I am aware that .UseRelationalNulls() exists, and I use it, but it only fixes the nullable comparison semantic (i.e. stops generating WHERE (a = b) OR (a IS NULL AND b IS NULL) and starts generating WHERE a = b like it should be).
It does not fix the null concatenation semantic.

  • Is there another configuration setting I am not aware of that brings back the database null concatenation semantic?
  • Is it a bug/overlook that .UseRelationalNulls() does not fix the null concatenation semantic? Should it be fixed to do so?

EF Core version: 8.0.4
Database provider: Microsoft.EntityFrameworkCore.SqlServer 8.0.4
Target framework: .NET 8.0
Operating system: Windows 10
IDE: Visual Studio 2022 17.9.6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants