I am using an ADO.NET Entity-Framework ObjectContext to access my data store.
I want the if values are set with empty strings, they should automatically become null.
I am using an ADO.NET Entity-Framework ObjectContext to access my data store.
I want the if values are set with empty strings, they should automatically become null.
Here is a solution for Entity Framework Core (tested in V2). Had to bang my way through the API due to limited documentation so there might be other ways to accomplish the same thing. Note that the original objects are modified using this method.
Just for completeness, here is the accepted answer written as a partial class instead of inherited. Note that this version also trims strings.
I have also shown the required
usings
because I find it frustrating when I copy'n'paste code and the IDE throws up some errors about type or namespace not found.I've just adapted the code above to the new release of Entity Framework 4.1 (DbContext).
If your using Entity Framework 4, you can use T4 templates to accomplish this. Just place this in the getter of each string property in your .tt template file, and it will replace empty strings with null and automatically trim strings. No need to use reflection.
Not that I'm aware of.
You could possibly write a class that inherited from
ObjectContext
and overrideSaveChanges()
to do that and use that instead ofObjectContext
in your x.objectlayer.cs / x.designer.csI actually found a better way to this, it's actually built in the system, plus it uses the internal ordinal metadata of the entities which are loaded anyway (I haven't tested the performance difference, but this should be hell of a lot faster than reflection):