如何使用Entity Framework修剪字符串? [英] How to trim string with Entity Framework?

查看:109
本文介绍了如何使用Entity Framework修剪字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使Entity Framework在存储所有字符串之前自动修剪所有字符串?

How to make Entity Framework automatically trim all strings before storing them in database?

推荐答案

您可以使用 IDbCommandInterceptor 拦截对数据库的所有调用。然后修剪所有传递的参数。

You can use IDbCommandInterceptor to intercept all calls to the database. Then trim any parameters being passed.

有关更多信息,请参见本文详细信息,尤其是如何注册拦截器。

See this article for more details and especially how to register the interceptor.

class TrimCommandInterceptor: IDbCommandInterceptor
{
  public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> ctx)
  {
    foreach (var p in command.Parameters)
    {
       if (p.Value is string)
         p.Value = ((string) p.Value).Trim();
    }
  }

  // Add all the other interceptor methods
}

这篇关于如何使用Entity Framework修剪字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆