c# – 如何使用Entity Framework修剪字符串?

前端之家收集整理的这篇文章主要介绍了c# – 如何使用Entity Framework修剪字符串?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何让Entity Framework在将所有字符串存储到数据库之前自动修剪它们?

解决方法

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

有关更多详细信息,请参阅this article,尤其是如何注册拦截器.

@H_404_8@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 }

猜你在找的C#相关文章