I have an ASP project which uses Code First
EF Core.
After Upgrading Dotnet7 to Dotnet8 and also EFCore Packages I’ve faced with an error
SqlException: Incorrect syntax near '$'.
This is query from profiler
exec sp_executesql N'SELECT [t].[id], [t].[idDatetime], [t].[IdDistPath], [t].[idLoadType], [t].[idLoader], [t].[idLoaderDriver], [t].[idShift], [t].[idTruck], [t].[idTruckDriver], [t].[level], [t].[pattern], [t].[Tonnage]
FROM [tbl_Loading] AS [t]
WHERE [t].[idShift] IN (
SELECT [s].[value]
FROM OPENJSON(@__shiftsAllIds_0) WITH ([value] int ''$'') AS [s]
)',N'@__shiftsAllIds_0 nvarchar(4000)',@__shiftsAllIds_0=N'[338769,338768]'
This is my OnConfiguring
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
optionsBuilder.UseSqlServer(_configuration.GetConnectionString("DefaultConnection")
, o => o.UseCompatibilityLevel(160));
}
I’ve tried to add another migration with Dotnet8 and Updated the database, but unfortunately it didn’t worked.
I’ve searched a lot in Stackoverflow community but none of threads are related to my issue.
Dotnet 8
SQL Server 2022 Version 16.0.1000.6
Windows 11
2
Answers
As Martin Smith comment I've executed this query on
master
databaseAnd saw that my database compatibility is 120 instead of 160
Seems I should update my database compatibility first
Microsoft Documentation
The SQL generated has changed as described here.
OPENJSON
requires SQL Server 2016+/compat level 130+.See the mitigation options in the above article