EntityFramework 和 oracle 包含在不使用变量的 Where 子句中

EntityFramework and oracle Contain in a Where clause not working with a variable

我正在使用 ASP.NET MVC 4.5.2、entityframework 6.1.3 和 Oracle 19c (19.3.0) 数据库。我遇到 EntityFramework 请求的奇怪行为。

请求如下:

List<College> testQuery1 = unitOfWork.GetRepository<College>()
                                     .AsQueryable()
                                     .Where(college => college.Nom.Contains("A"))
                                     .ToList();

我得到以下结果 => testQuery1.Count = 172。但是有了这个要求:

string queryParameter = "A";

List<College> testQuery2 = unitOfWork.GetRepository<College>()
                                     .AsQueryable()
                                     .Where(college => college.Nom.Contains(queryParameter))
                                     .ToList();

结果是testQuery2.Count = 0。这个问题只发生在我的 Oracle 19.3.0.0 数据库上,Oracle 19.10.0.0 或 12.1.2.0 一切正常,没有差异。以下是 entityframework 生成的查询。对于 testQuery1:

SELECT 
"Extent1"."ID" AS "ID", 
"Extent1"."ID_LIEU_RDV" AS "ID_LIEU_RDV", 
"Extent1"."ID_ADRESSE" AS "ID_ADRESSE", 
"Extent1"."DSIT_NOSITE" AS "DSIT_NOSITE", 
"Extent1"."CODE_COLLEGE" AS "CODE_COLLEGE", 
"Extent1"."NOM" AS "NOM", 
"Extent1"."CREATED_BY" AS "CREATED_BY", 
"Extent1"."CREATED_DATE" AS "CREATED_DATE", 
"Extent1"."MODIFIED_BY" AS "MODIFIED_BY", 
"Extent1"."MODIFIED_DATE" AS "MODIFIED_DATE"
FROM "DSS"."DSS_COLLEGE" "Extent1"
WHERE ("Extent1"."NOM" LIKE '%A%')

对于 testQuery2:

SELECT 
"Extent1"."ID" AS "ID", 
"Extent1"."ID_LIEU_RDV" AS "ID_LIEU_RDV", 
"Extent1"."ID_ADRESSE" AS "ID_ADRESSE", 
"Extent1"."DSIT_NOSITE" AS "DSIT_NOSITE", 
"Extent1"."CODE_COLLEGE" AS "CODE_COLLEGE", 
"Extent1"."NOM" AS "NOM", 
"Extent1"."CREATED_BY" AS "CREATED_BY", 
"Extent1"."CREATED_DATE" AS "CREATED_DATE", 
"Extent1"."MODIFIED_BY" AS "MODIFIED_BY", 
"Extent1"."MODIFIED_DATE" AS "MODIFIED_DATE"
FROM "DSS"."DSS_COLLEGE" "Extent1"
WHERE ("Extent1"."NOM" LIKE :p__linq__0 ESCAPE '\')

其中 p_linq_0 等于“%A%”。如果我直接在数据库上执行这些查询,结果是一样的,两次都是 172 个结果。

如果我也将 Contains 替换为 StartsWith 和 EndsWith,就会发生这种情况。你知道这种奇怪的行为可能来自哪里吗?

提前感谢您的任何见解。

我们终于找到问题所在了。如果有人遇到同样的问题,我会与您分享。

创建我们的 19.3.0 Oracle 数据库后,NLS 参数“NLS_CHARACTERSET”的值有误,已更改。这不是一个好主意,因为 Oracle 不建议在创建数据库后更改 NLS 参数,而这正是我们问题的根源。解决方案是,正如您想象的那样,回滚到旧值或创建另一个具有正确值的数据库并迁移数据