如何使实体框架在将它们存储到数据库之前自动修剪所有字符串?
您可以使用IDbCommandInterceptor
拦截对数据库的所有调用。然后修剪传递的所有参数。
See 本文 http://www.entityframeworktutorial.net/EntityFramework6/database-command-interception.aspx了解更多详细信息,特别是如何注册拦截器。
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
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)