使用.NET 8 RC2和EF核心

我创建了一个动态查询并执行了导致以下错误的查询

Microsoft.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near '$'.
Incorrect syntax near '$'.
Incorrect syntax near '$'.

如果我运行.ToQueryString()来查看生成的查询,我会得到这样的结果.

DECLARE @__startDate_0 smalldatetime = '2023-10-26T00:00:00';
DECLARE @__endDate_1 smalldatetime = '2023-10-26T00:00:00';
DECLARE @__siteNos_2 nvarchar(4000) = N'[15,42,56,74,89,98,102,104,109,113,114,115,116,118,120,121,122,123,124,124,125,127,128,129,130,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,156,157,158,159,161,162,163,164,165,167,169,170,171,172,173,175,176,177,178,179,180,181,182,183,185,186,187,188,189,190,191,192,193,194,195,196,197,199,200,201,202,205,206,207,208,209,210,211,212,213,214,215,216,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,244,245,246,247,248,249,250,251,252,254,255,256,257,258,259,260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,279,280,281,282,283,284,285,286,287,288,289,290,291,292,293,295,296,296,297,300,301,303,304,305,306,307,308,309,310,311,312,313,316,317,318,319,320,321,322,323,324,325,326,328,331,333,334,335,338,339,340,341,342,343]';
DECLARE @__startTime_3 time = '07:00:00';  
DECLARE @__endTime_4 time = '22:00:00';  
DECLARE @__upperLimit_5 smallint = CAST(30000 AS smallint);  
DECLARE @__lowerLimit_6 smallint = CAST(0 AS smallint);
DECLARE @__stationIds_7 nvarchar(4000) = N'[117,116,98]';
DECLARE @__courseIds_8 nvarchar(4000) = N'[7]';

SELECT [o].[order_no], [o].[Bumped], [o].[Check_no], [o].[checkback], [o].[Course], [o].[Expoed], [o].[item_count], [o].[manual_hold], [o].[ODate], [o].[Order_DateTime], [o].[Order_Index], [o].[Server_no], [o].[Site_no], [o].[StartTime], [o].[Station_no], [o].[Table_no]
FROM [Orders] AS [o]
WHERE [o].[Order_DateTime] IS NOT NULL AND CONVERT(date, [o].[Order_DateTime]) >= @__startDate_0 AND CONVERT(date, [o].[Order_DateTime]) <= @__endDate_1 AND [o].[Site_no] IN (
SELECT [s].[value]
FROM OPENJSON(@__siteNos_2) WITH ([value] smallint '$') AS [s]  
) AND [o].[item_count] > CAST(0 AS tinyint) AND CONVERT(time, [o].[Order_DateTime]) >= @__startTime_3 AND CONVERT(time, [o].[Order_DateTime]) <= @__endTime_4 AND [o].[Expoed] <= @__upperLimit_5 AND [o].[Expoed] >= @__lowerLimit_6 AND CAST([o].[Station_no] AS smallint) NOT IN (
SELECT [s0].[value]
FROM OPENJSON(@__stationIds_7) WITH ([value] smallint '$') AS [s0]
 ) AND CAST([o].[Course] AS smallint) IN (
SELECT [c].[value]
FROM OPENJSON(@__courseIds_8) WITH ([value] smallint '$') AS [c]
)

我可以看到‘$’,我假设EF Core正在抱怨,但是EF core生成了查询,那么我该如何解决这个问题呢?

推荐答案

我看到一些数据库是110,而我们创建的新数据库是150

将值传递给上下文选项(例如,在DI中使用AddDbContext注册时):

opts
   .UseSqlServer(@"<CONNECTION STRING>", o => o.UseCompatibilityLevel(110)) // or 150

Mitigations部分的突破性变化文件.

或者,您可以try 升级数据库以支持新功能(请参见ALTER DATABASE SET COMPATIBILITY_LEVEL).

Csharp相关问答推荐

Blazor:用参数创建根路径

MongoDB实体框架核心:表达必须可写

Monty Hall游戏节目模拟给我50/50的结果

ITypeLib2.GetLibStatistics()在C#中总是抛出AccessViolationException

Polly v8—使用PredicateBuilder重试特定的状态代码

如何注册接口类型,类型<>

在.NET 7.0 API控制器项目中通过继承和显式调用基类使用依赖项注入

使用Entity Framework6在对象中填充列表会导致列表大多为空

如何在用户在线时限制令牌生成?

是否由DI容器自动处理由ActivatorUilties.CreateInstance()创建的服务?

HelperText属性不支持复杂内容(混合C#和标记)

在被Interactive Server切换后,Blazor SSR页面无法正确加载JS

Blazor Server/.NET 8/在初始加载时调用异步代码是否冻结屏幕,直到第一次异步调用完成?

从Base64转换为不同的字符串返回相同的结果

如何从Azure函数使用Graph API(SDK 5.35)中的[FindMeetingTimes]

工厂类是如何在.NET 8中注册的?

我是否应该注销全局异常处理程序

如何使用.NET 8.0中新的CompositeFormat类?

SendInput无法在C#中正确模拟键盘

需要帮助才能在图片框中显示C#(WinForm)中纯资源DLL中的图像