Tuesday, September 24, 2013

EF Empty Strings in Non-Nullable Fields

When using Entity Framework, sometimes it is desirable to have non-nullable string fields.  The problem is that if the user leaves the field blank, EF will complain that a value is required in the field.   To fix this, create a metadata class for the entity you are working with.   Then decorate the field with this attribute:
[DisplayFormat(ConvertEmptyStringToNull=false)]
This will allow you to save the entity with an empty string in this field.

No comments:

Post a Comment