How to trim a string using Entity Framework?

How to force Entity Framework to automatically trim all rows before storing them in the database?

+6
source share
1 answer

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

See this article for more details and especially how to register an 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 } 
+8
source

All Articles