C# EF查询到Oracle抛出“;ORA-12704:字符集不匹配“;

C# EF查询到Oracle抛出“;ORA-12704:字符集不匹配“;,c#,.net,oracle,entity-framework,C#,.net,Oracle,Entity Framework,我试图在Oracle的EF中组合一些列,然后对这些列执行.Contains(): public IEnumerable<User> SearchUsers(string search) { search = search.ToLower(); return _securityUow.Users .Where(u => (u.FirstName.ToLower() + " " + u.LastName.ToLower() + " (" +

我试图在Oracle的EF中组合一些列,然后对这些列执行
.Contains()

public IEnumerable<User> SearchUsers(string search)
{
    search = search.ToLower();

    return _securityUow.Users
            .Where(u => (u.FirstName.ToLower() + " " + u.LastName.ToLower() + " (" + u.NetId.ToLower() + ")").Contains(search))
            .OrderBy(u => u.LastName)
            .ThenBy(u => u.FirstName)
            .AsEnumerable();
}
public IEnumerable SearchUsers(字符串搜索)
{
search=search.ToLower();
return\u securityUow.Users
其中(u=>(u.FirstName.ToLower()+“”+u.LastName.ToLower()+“”)(“+u.NetId.ToLower()+”)。包含(搜索))
.OrderBy(u=>u.LastName)
.ThenBy(u=>u.FirstName)
.AsEnumerable();
}
但是,我得到了一个例外:

{
  "Message": "An error has occurred.",
  "ExceptionMessage": "An error occurred while executing the command definition. See the inner exception for details.",
  "ExceptionType": "System.Data.Entity.Core.EntityCommandExecutionException",
  "StackTrace": "   at SoftwareRegistration.WebUI.Controllers.Api.V1.UserContactController.Lookup(String search) in C:\LocalRepository\OnlineSupport\SoftwareRegistration\trunk\release\SoftwareRegistration\SoftwareRegistration.WebUI\Controllers\Api\V1\UserContactController.cs:line 40\r\n   at lambda_method(Closure , Object , Object[] )\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n   at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n   at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()",
  "InnerException": {
    "Message": "An error has occurred.",
    "ExceptionMessage": "ORA-12704: character set mismatch",
    "ExceptionType": "Oracle.ManagedDataAccess.Client.OracleException",
    "StackTrace": "   at OracleInternal.ServiceObjects.OracleCommandImpl.VerifyExecution(OracleConnectionImpl connectionImpl, Int32& cursorId, Boolean bThrowArrayBindRelatedErrors, OracleException& exceptionForArrayBindDML, Boolean& hasMoreRowsInDB, Boolean bFirstIterationDone)\r\n   at OracleInternal.ServiceObjects.OracleCommandImpl.ExecuteReader(String commandText, OracleParameterCollection paramColl, CommandType commandType, OracleConnectionImpl connectionImpl, OracleDataReaderImpl& rdrImpl, Int32 longFetchSize, Int64 clientInitialLOBFS, OracleDependencyImpl orclDependencyImpl, Int64[] scnForExecution, Int64[]& scnFromExecution, OracleParameterCollection& bindByPositionParamColl, Boolean& bBindParamPresent, Int64& internalInitialLOBFS, OracleException& exceptionForArrayBindDML, Boolean isDescribeOnly, Boolean isFromEF)\r\n   at Oracle.ManagedDataAccess.Client.OracleCommand.ExecuteReader(Boolean requery, Boolean fillRequest, CommandBehavior behavior)\r\n   at Oracle.ManagedDataAccess.Client.OracleCommand.ExecuteDbDataReader(CommandBehavior behavior)\r\n   at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)\r\n   at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<Reader>b__c(DbCommand t, DbCommandInterceptionContext`1 c)\r\n   at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)\r\n   at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext)\r\n   at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(CommandBehavior behavior)\r\n   at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)\r\n   at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)"
  }
}
{
“消息”:“发生错误。”,
“ExceptionMessage”:“执行命令定义时出错。有关详细信息,请参阅内部异常。”,
“ExceptionType”:“System.Data.Entity.Core.EntityCommandExecutionException”,
“StackTrace:”在C:\LocalRepository\OnlineSupport\SoftwareRegistration\trunk\release\SoftwareRegistration\SoftwareRegistration.WebUI\Controllers\Api\V1\UserContactController.cs中的SoftwareRegistration.Api.V1.UserContactController.Lookup(字符串搜索)中:第40行\r\n lambda\u方法(闭包、对象、对象[])\r\n位于System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.c\u DisplayClass10.b\u 9(对象实例,对象[]方法参数)\r\n位于System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(对象实例,对象[]参数)\r\n在System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext,IDictionary`2参数,CancellationToken CancellationToken)\r\n--从引发异常的上一个位置开始的堆栈结束跟踪---\r\n在System.Runtime.CompilerServices.TaskWaiter.ThrowForOnSuccess(任务任务)\r\n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\r\n在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n在System.Web.Http.Controllers.ApiControllerActionInvoker.d\u 0.MoveNext()\r\n----从引发异常的上一个位置开始的堆栈结束跟踪----\r\n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务任务)\r\n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务任务任务)\r\n在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n位于System.Web.Http.Controllers.ActionFilterResult.d\u 2.MoveNext()\r\n---来自引发异常的上一个位置的堆栈结束跟踪----\r\n位于System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(任务任务任务)\r\n位于System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务任务任务任务任务)\r\n位于System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n位于System.Web.Http.Dispatcher.HttpControllerDispatcher.d_u1.MoveNext()“,
“内部异常”:{
“消息”:“发生错误。”,
“例外消息”:“ORA-12704:字符集不匹配”,
“ExceptionType”:“Oracle.ManagedDataAccess.Client.OracleException”,
“StackTrace”:“在OracleInternal.ServiceObjects.OracleCommandImpl.VerifyExecution(OracleConnectionImpl connectionImpl,Int32&cursorId,Boolean bThrowArrayBindRelatedErrors,OracleException&ExceptionForArrayBindML,Boolean&hasMoreRowsInDB,Boolean BfirstitionDone)\r\n在OracleInternal.ServiceObjects.OracleCommandImpl.ExecuteReader(字符串commandText、OracleParameterCollection paramColl、CommandType CommandType、OracleConnectionImpl connectionImpl、OracleDataReaderImpl&rdrImpl、Int32 longFetchSize、Int64 clientInitialLOBFS、OracleDependencyImpl或LdependencyImpl、Int64[]SCnforexection、Int64[]&scnFromExecution、OracleParameterCollection和bindByPositionParamColl、Boolean和bBindParamPresent、Int64和internalInitialLOBFS、OracleException和ExceptionForArrayBindML、Boolean IsDescriptionOnly、Boolean isFromEF)\r\n位于Oracle.ManagedDataAccess.Client.OracleCommand.ExecuteReader(Boolean requery、Boolean fillRequest、CommandBehavior)\r\n位于Oracle.ManagedDataAccess.Client.OracleCommand.ExecutedDataReader(CommandBehavior行为)\r\n位于System.Data.Common.DbCommand.ExecuteReader(CommandBehavior行为)\r\n位于System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.b_uC(DbCommand t,DbCommandInterceptionContext`1 c)\r\n在System.Data.Entity.Infrastructure.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget目标,Func`3操作,TInterceptionContext拦截Context,Action`3正在执行,Action`3已执行)\r\n在System.Data.Entity.Infrastructure.Depatcher.DbCommandDispatcher.Reader(DbCommand命令,DBCommandInterceptOnContext InterceptOnContext)\r\n位于System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(CommandBehavior行为)\r\n位于System.Data.Common.DbCommand.ExecuteReader(CommandBehavior行为)\r\n位于System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStorommands(EntityCommand EntityCommand,CommandBehavior)
}
}
我查询的列在Oracle中都是VARCHAR2(128)类型

我在另一个项目中也使用了完全相同的代码,它也可以工作。唯一的区别是,对于工作的项目,我使用的是
Oracle.DataAccess
,对于不工作的项目,我使用的是
Oracle.ManagedDataAccess
(我无法在此项目中使用
Oracle.DataAccess
)。因此,我认为托管驱动程序中存在错误/问题


我对解决方案或变通方法持开放态度。

我最终让本()的作者更新了问题,他发布了一个使用拦截器的变通方法,我将在这里详细介绍一下

首先,我修饰了DBContext以加载配置。如果您有配置,您可以跳过此操作,只需添加到配置中:

[DbConfigurationType(typeof(MyDbConfiguration))]
public partial class MyContext : DbContext
创建配置类:

public class MyDbConfiguration : DbConfiguration
{
    public MyDbConfiguration()
    {
        this.AddInterceptor(new NVarcharInterceptor()); //add this line to existing config.
    }
}
接下来,创建拦截器:

public class NVarcharInterceptor : IDbCommandInterceptor
{
    public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
    {
        if (command != null && !string.IsNullOrWhiteSpace(command.CommandText))
            command.CommandText = command.CommandText.Replace("N''", "''");
    }

    public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
    {
        if (command != null && !string.IsNullOrWhiteSpace(command.CommandText))
            command.CommandText = command.CommandText.Replace("N''", "''");
    }

    public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
    {
        if (command != null && !string.IsNullOrWhiteSpace(command.CommandText))
            command.CommandText = command.CommandText.Replace("N''", "''");
    }

    public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
    {
        if (command != null && !string.IsNullOrWhiteSpace(command.CommandText))
            command.CommandText = command.CommandText.Replace("N''", "''");
    }

    public void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
    {
        if (command != null && !string.IsNullOrWhiteSpace(command.CommandText))
            command.CommandText = command.CommandText.Replace("N''", "''");
    }

    public void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
    {
        if (command != null && !string.IsNullOrWhiteSpace(command.CommandText))
            command.CommandText = command.CommandText.Replace("N''", "''");
    }
}
公共类NVARCHIRINTERCEPTOR:IDbCommandInter