EfCore 6: string.IndexOf throws NullReferenceException when applied on the result of a DbFunction #29204
Labels
area-query
closed-fixed
The issue has been fixed and is/will be included in the release indicated by the issue milestone.
customer-reported
Servicing-approved
type-bug
Milestone
Description
Imagine you have a DbFunction of the form:
If you were to apply an IndexOf function to the result of this function (which would be a string), it would throw a "NullReferenceException: Object reference not set to an instance of an object" error.
This used to work in .Net 3.1, but throws the NullReferenceException since .Net 5.
Could this be related to dotnet/runtime#43736? I'm not sure how, though...
Workaround
A workaround (also illustrated in the sample I provided) would be to create a scalar function
Then create another DbFunction of it:
And replace the usual string.IndexOf call with the DbFunction created above:
Code
A repository reproducing the bug can be found here: https://github.com/tedchirvasiu/EfCoreIndexOfStringBug. The relevant code is situated in
Program.cs
for .Net 6 orStartup.cs
for .Net 5 and .Net Core 3.1I showcases how it works in .Ef Core 3.1 but the same code does not in .Ef Core 5 and .Ef Core 6.
It also includes a sample for the workaround.
Stack traces
Provider and version information
EF Core version: 6.0.9 (and 5.0.17)
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6 (and .NET 5)
Operating system: Windows 10 Pro 19043.2006
IDE: Microsoft Visual Studio Community 2022 (64-bit) 17.2.6
The text was updated successfully, but these errors were encountered: