-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
DbFunction definition returning nullable value type causes incorrect SQL to get generated #29585
Comments
The problem is with the DbFunction configuration of the ToGuid function: modelBuilder.HasDbFunction(type.GetMethod(nameof(ToGuid))!)
.HasTranslation(t => new SqlFunctionExpression(
functionName: "CONVERT",
arguments: new[] { new SqlFragmentExpression("UNIQUEIDENTIFIER"), t[0] },
nullable: true,
argumentsPropagateNullability: new[] { false, true },
typeof(Guid?), <-- Change to Guid
typeMapping: null))
.IsBuiltIn(); Although the function may return null, in EF's SQL tree, the CLR types are never nullable (since nullability is handled differently on the SQL side). Changing the function definition to return Note that we have a debug assertion against this, but of course it doesn't occur in Release. We should add For reference, the reason this started failing in 7.0 is f1bcf13, which changed the |
Thank you so much, I wouldn't have figured it out on my own. |
This is now throwing an exception in EF Core 8.0 which wasn't present in EF Core 7. However, it doesn't seem to be documented as a "breaking change", which it technically is. |
@RichardD2 When something was not working correctly in a previous version and we fix that, as was the case here, we generally don't consider that a breaking change since your code wasn't working correctly before. |
@ajcvickers In my case, I was using It was even mostly working for other people: The only issue reported with EF7 was a missing Changing that to throw an exception at runtime certainly seems like a breaking change to me. :) |
…should *not* be nullable.
Note for triage: make sure we are not being overly aggressive validating this. |
@RichardD2 I checked with the team, and while this may have been working for you in some cases, that was accidental, and the code should be changed going forward. |
I have AsQueryable extension method that solves SQL Server cache pollution problem using STRING_SPLIT approach. The following invocation has different results depending of TargetFramework
For EFCore 6.0.11 it produces
exec sp_executesql N'SELECT [c].[CustomerId] FROM [Customers] AS [c] WHERE EXISTS ( SELECT 1 FROM STRING_SPLIT(@__source_1, @__separator_2) AS [s] WHERE CONVERT(UNIQUEIDENTIFIER, [s].[Value]) = [c].[CustomerId])',N'@__source_1 nvarchar(4000),@__separator_2 nvarchar(4000)',@__source_1=N'009cf046-a859-4849-8de7-9854bd00f7de,b38c95f2-e5aa-4926-a315-a23ce6b6578c',@__separator_2=N','
and for EFCore 7.0.0 it has
SELECT [c].[CustomerId] FROM [Customers] AS [c] WHERE 0 = 1
The full code of the program is in the attachment
ConsoleApp1.zip
The text was updated successfully, but these errors were encountered: