如何防止Entity-Framework生成N'..'带前缀的 unicode 字符串?

发布于 2024-11-27 07:36:28 字数 280 浏览 1 评论 0原文

我使用 EF 4.1 Code-First ,
问题是:EF 默认生成所有带有 N'..' 前缀的 unicode 字段。像这样 : 执行sp_executesql 不选择... 从 ... WHERE [Title] LIKE @p__linq__0 ESCAPE N''~''', N'@p__linq__0 nvarchar(4000)', @p__linq__0=N'%...%'

但它导致我在某些字符上出现一些问题。我想知道是否有办法阻止EF添加N前缀?

I use EF 4.1 Code-First ,
the problem is: EF generates all the unicode fields with N'..' prefix by default. like this :
exec sp_executesql
N'SELECT ...
FROM ...
WHERE [Title] LIKE @p__linq__0 ESCAPE N''~''',
N'@p__linq__0 nvarchar(4000)',
@p__linq__0=N'%...%'

but it cause me some problems in some characters. I want to know if there is a way to prevent EF of adding N prefix or not?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪漫之都 2024-12-04 07:36:28

您可以将字符串包装在 AsNonUnicode 方法中,如 中所述http://msdn.microsoft.com/en-us/library/system.data.objects.entityfunctions.asnonunicode.aspx 这将生成普通字符串。

You can wrap your strings in AsNonUnicode method as mentioned at http://msdn.microsoft.com/en-us/library/system.data.objects.entityfunctions.asnonunicode.aspx this will generate normal strings.

夏见 2024-12-04 07:36:28

另一种解决方案是使用 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);
    }
}

Another solution would be to use CommandInterceptors and modify the resulting sql query before it gets executed.

I had similar problems with an oracle database and ODP.net provider.
AsNonUnicode didn't solved my problem.

EF 6 provides the ability to intercept the context using IDbCommandInterceptor before and after it performs the ExecuteNonQuery, ExecuteScalar, ExecuteReader operations to the database. You will need to configure the interceptor either by using config file or code-based configuration.

Config file:

<entityFramework>
    <interceptors>
      <interceptor type="EfSample.EfCommandInterceptor, EfSample">
      </interceptor>
    </interceptors>
</entityFramework>

Code-based config:

public sealed class EntityFrameworkConfiguration : DbConfiguration
{
     public EntityFrameworkConfiguration ()
     {
         this.AddInterceptor(new EfCommandInterceptor());
     }
}

Create the CommandInterceptor as shown below:

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文