【问题标题】:Entity Framework 6.1.3 and Oracle produces sql with Unicode on NonUnicode columnEntity Framework 6.1.3 和 Oracle 在 NonUnicode 列上生成带有 Unicode 的 sql
【发布时间】:2015-07-22 18:17:15
【问题描述】:

我将实体框架 6.1.3 与 Oracle ManagedDataAccess 12.1.022 和 Oracle.managedDataAccess.EntityFramework 12.1.022(NuGet 包)一起使用(图 1)。我的项目首先是一个数据库,并将模型导入到运行 T4 代码的.edmx 文件中。

图1.

数据库使用varchar2 列,.edmx 文件将它们识别为非Unicode。 (图二、三)

图 2。

图 3.

运行查询时,我收到一个 oracle 错误ORA-12704: character set mismatch。这是查询:

var emp2 = db.EMPLOYEEs
                .Where(s => s.FIRST_NM.ToUpper().StartsWith(term.ToUpper()) || s.LAST_NM.ToUpper().StartsWith(term.ToUpper()))
                .Select(c => new
                 {
                     label = c.FIRST_NM + " " + c.LAST_NM,
                     value = c.FIRST_NM + " " + c.LAST_NM
                 });

使用.ToTraceString()可以看到正在发送的sql是:

SELECT 
1 AS "C1", 
((((CASE WHEN ("Extent1"."FIRST_NM" IS NULL) THEN N'' /* Unicode here for null value */
ELSE "Extent1"."FIRST_NM" END)||(' ')))||(CASE WHEN ("Extent1"."LAST_NM" IS NULL) THEN N''
 ELSE "Extent1"."LAST_NM" END)) AS "C2", 
((((CASE WHEN ("Extent1"."FIRST_NM" IS NULL) THEN N'' ELSE "Extent1"."FIRST_NM" END)||(' ')))||(CASE WHEN ("Extent1"."LAST_NM" IS NULL) THEN N'' ELSE "Extent1"."LAST_NM" END)) AS "C3"
FROM "RISKMGMT"."EMPLOYEE" "Extent1"
WHERE ((( NVL(INSTR(UPPER("Extent1"."FIRST_NM"), UPPER(:p__linq__0)), 0) ) = 1) OR (( NVL(INSTR(UPPER("Extent1"."LAST_NM"), UPPER(:p__linq__1)), 0) ) = 1))
 /** parameters **/ 
 p__linq__0[System.String] = j

 /** parameters **/ 
 p__linq__1[System.String] = j

select 子句的N'' 部分会导致错误。如果我删除N,查询将起作用。我通过搜索发现这是 EF4 中的一个问题,但目前没有。知道为什么会发生这种情况以及解决它的方法,以便生成的 SQL 不会是 Unicode 吗?

【问题讨论】:

  • 您的模型是否有 Unicode 设置? stackoverflow.com/questions/17258901/…
  • 是的。我更新了图。 3 显示底部的Unicode属性设置为false。
  • 我在查询中遇到了 unicode 字符问题,更新 oracle 提供程序解决了这个问题。试试 odac 12c 或更高版本。

标签: c# oracle entity-framework unicode


【解决方案1】:

我能够通过将查询分成两个不同的部分并在第一个 where 子句的末尾添加 .ToArray() 来解决此问题。我想返回一个 JSON 对象,所以这是我的解决方案。

db.EMPLOYEEs
  .Where(s => s.FIRST_NM.ToUpper().StartsWith(term.ToUpper())
           || s.LAST_NM.ToUpper().StartsWith(term.ToUpper()))
 .ToArray();

然后格式化 JSON 对象:

return Json(matching.Select(m => new
        {
            id = m.EMPLID,
            value = m.FIRST_NM + " " + m.LAST_NM,
            label = String.Concat(m.FIRST_NM, " ", m.LAST_NM),
        }), JsonRequestBehavior.AllowGet);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-24
    • 2015-02-08
    • 1970-01-01
    • 2014-08-18
    • 2016-07-12
    • 2017-10-08
    • 1970-01-01
    相关资源
    最近更新 更多