本文介绍了ODP.Net托管驱动程序 - ORA-12704:生成代码中的字符集不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Oracle Managed Driver(v12.1.2400)作为我的实体框架驱动程序,目前在执行期间看到一个 ORA-12704:字符集不匹配错误

I am currently using the Oracle Managed Driver (v12.1.2400) as my Entity Framework driver, and am currently seeing a ORA-12704: character set mismatch error during execution.

我使用的LINQ-> SQL代码如下:

The LINQ->SQL code I am using is as follows:

from c in CUSTOMER.AsNoTracking()
where c.ACCOUNT.Contains("DE")
   && c.DELETED == "N"
orderby (c.FORENAME + c.SURNAME)
select new { c.ACCOUNT, c.FORENAME, c.SURNAME})

这是创建以下SQL:

SELECT "Project1"."C2" AS "C1",
"Project1"."ACCOUNT" AS "ACCOUNT",
"Project1"."FORENAME" AS "FORENAME",
"Project1"."SURNAME" AS "SURNAME"
FROM (
      SELECT(  (CASE WHEN ("Extent1"."FORENAME" IS NULL) THEN N''
                     ELSE "Extent1"."FORENAME" END)
             ||(CASE WHEN ("Extent1"."SURNAME" IS NULL) THEN N''
                     ELSE "Extent1"."SURNAME" END)) AS "C1",
             "Extent1"."ACCOUNT" AS "ACCOUNT",
             "Extent1"."FORENAME" AS "FORENAME",
             "Extent1"."SURNAME" AS "SURNAME",
             1 AS "C2"
      FROM "TEST"."CUSTOMER" "Extent1"
      WHERE (("Extent1"."ACCOUNT" LIKE '%DE%')
             AND ('N' = "Extent1"."DELETED")))  "Project1"
ORDER BY "Project1"."C1" ASC;

当我调试该SQL时,我可以看到的问题是SQL正在使用<$ c $ CASE 部分中的c> N如果我删除前面的 N 只留下'',那么sql列不是unicode,那么sql的工作原理

When I debug that SQL, I can see the issue is that the SQL is using N'' in the CASE sections. AS teh columns are not unicode, if I remove the preceding N to leave just '' then the sql works as expected.

有什么办法可以防止这种默认?

Is there any way I can prevent this defaulting?

目前所有的数据库列都是 VARCHAR ,并以C#形式建模为 string

两列的代码第一个映射如下:

All db columns are currently VARCHAR, and are modeled in C# as string.
Code first mappings for the two columns are as follows:

this.Property(t => t.FORENAME).HasColumnName("FORENAME").IsUnicode(false).HasMaxLength(35);
this.Property(t => t.SURNAME).HasColumnName("SURNAME").IsUnicode(false).HasMaxLength(35);

我期待着 IsUnicode(false)

FYI,当我使用EF5和非管理驱动程序时,这用于工作。

此外, Devart dotConnectForOracle驱动程序没有这个问题,所以我认为这是一个Oracle驱动程序的错误。

FYI, this used to work when I used EF5 and the non-managed driver.
In addition, the Devart dotConnectForOracle drivers dont have this issue, so I am thinking this is a bug in the Oracle drivers.

推荐答案

我从来没有找到适当的解决方案,但是我确实发现一个解决方法很好。

I never did find the proper solution to this, however I did find a workaround that works well.

我创建了一个拦截器类 NVarcharInterceptor 实现 IDbCommandInterceptor ,并覆盖所有 ..执行(..)方法以包含以下代码: / p>

I created an Interceptor class NVarcharInterceptor implementing IDbCommandInterceptor, and overrode all of the ..Executing(..) methods to contain the following code:

if (command != null && !string.IsNullOrWhiteSpace(command.CommandText))
    command.CommandText = command.CommandText.Replace("N''", "''");

这有效地删除了任何不需要的 NVarchar 引用,从我的 DbContext 执行的任何命令。

This effectively removes any of the unwanted NVarchar references, from any command being executed on my DbContext.

要添加拦截器,我添加了以下代码通过 DBConfiguration class:

To add the interceptor, I added the following code to by DBConfiguration class:

this.AddInterceptor(new NVarcharInterceptor());

这篇关于ODP.Net托管驱动程序 - ORA-12704:生成代码中的字符集不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 12:54
查看更多