Is there an option to make Entity Framework revert empty strings to null?

后端 未结 8 897
时光说笑
时光说笑 2021-02-06 09:30

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.

8条回答
  •  迷失自我
    2021-02-06 09:49

    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.

    <#+ if (primitiveProperty.TypeUsage.ToString().Split('.').Last() == "String") { #>
       if (value == "") value = null;
       else value = value.Trim();
    <#+ } #>
    

提交回复
热议问题