我使用EF 4.1 Code-First,
问题是: EF生成所有带有N'..‘的unicode字段。默认情况下为前缀。像这样:exec sp_executesql N'SELECT ... FROM ... WHERE [Title] LIKE @p__linq__0 ESCAPE N''~''', N'@p__linq__0 nvarchar(4000)', @p__linq__0=N'%...%'
发布于 2011-07-31 18:54:30
您可以将字符串包装在http://msdn.microsoft.com/en-us/library/system.data.objects.entityfunctions.asnonunicode.aspx中提到的AsNonUnicode方法中,这将生成正常的字符串。
发布于 2015-09-17 15:47:11
另一种解决方案是使用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());
}
}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);
}
}https://stackoverflow.com/questions/6888903
复制相似问题