別の解決策は、CommandInterceptors を使用し、結果の SQL クエリを実行前に変更することです。
Oracle データベースと ODP.net プロバイダーで同様の問題が発生しました。AsNonUnicode は私の問題を解決しませんでした。
EF 6 は、データベースに対して ExecuteNonQuery、ExecuteScalar、ExecuteReader 操作を実行する前後に、IDbCommandInterceptor を使用してコンテキストをインターセプトする機能を提供します。構成ファイルまたはコードベースの構成を使用して、インターセプターを構成する必要があります。
構成ファイル:
<entityFramework>
<interceptors>
<interceptor type="EfSample.EfCommandInterceptor, EfSample">
</interceptor>
</interceptors>
</entityFramework>
コードベースの構成:
public sealed class EntityFrameworkConfiguration : DbConfiguration
{
public EntityFrameworkConfiguration ()
{
this.AddInterceptor(new EfCommandInterceptor());
}
}
以下に示すように CommandInterceptor を作成します。
public sealed class EfCommandInterceptor
: DbCommandInterceptor
{
/// <summary>
/// Called when Reader is executing.
/// </summary>
/// <param name="command"></param>
/// <param name="interceptionContext"></param>
/// <inheritdoc />
public override void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
if(command.CommandText.Contains("N''"))
{
command.CommandText = command.CommandText.Replace("N''", "''");
}
base.ReaderExecuting(command, interceptionContext);
}
}