For string fields In EF < 7.0, there's no simple way to store the distinction between knowing a string value is empty, vs not knowing what the string value is. In other words EF < 7.0 assumes an empty string is the same thing as a null string.
How will EF7+ allow for storing a NULL in VARCHAR() type database field to signify that the string value is not know, v.s. storing a VARCHAR value of length 0; i,e, an empty string, signifying the value is know and it is empty?
It is the first time I hear about this as a limitation in previous versions of EF. Could you provide a code snippet showing what you mean? FWIW, there are database engines (such as Oracle) and even UI controls that conflate null and empty strings, but EF doesn't as far as I am aware.
I autocreated a code based model from a datasource. There was a VARCHAR column marked as NOT NULL from the datasources schema, which EF translated into a string field with a RequiredAttribute. However, for this field, EF converts an empty string value to a null value, which then cause validation to fail. Adding the AllowEmptyStrings option to the RequiredAttribute then also requires adding the ConvertEmptyStringToNull option to the DisplayFormatAttribute in order for EF to just leave the value alone even when being set only from code (never displayed); i.e. null is NULL, and different from an empty string being a NON NULL zero length VARCHAR. I suppose adding those two options is not exactly difficult.
My question is, will EF7 also assume an empty string is the same thing as a null string unless otherwise explicitly told not to via 2 or more Attributes? It's not intuitive that the default behavior when setting a field to an empty string in code, is for EF to then convert it to NULL and then complain a required field does not have a value, when it did and its value was an empty string.
@phestermcs As I mentioned before EF will not replace an empty string on a property with a null value.
The errors you are seeing are a consequence of the data validation feature which both EF5 and EF6 use by default on DbContext.SaveChanges(). In this particular case the validation logic in the System.ComponentModel.DataAnnotations.RequiredAttribute placed on a string property will by default produce a validation error for an empty string the same way it would for a null value.
There are two easy ways you can change this:
``` C#
public class Person
{
public int Id { get; set; }
[Required(AllowEmptyStrings = true)]
public string Name { get; set; }
}
1. Disable the validation feature (assuming you don't need it):
``` C#
context.Configuration.ValidateOnSaveEnabled = false;
context.SaveChanges();
EF7 will not automatically perform validation on SaveChanges(). It will leave it to other frameworks or to the application to do it.
Thank you kindly for articulating.
Most helpful comment
@phestermcs As I mentioned before EF will not replace an empty string on a property with a null value.
The errors you are seeing are a consequence of the data validation feature which both EF5 and EF6 use by default on DbContext.SaveChanges(). In this particular case the validation logic in the
System.ComponentModel.DataAnnotations.RequiredAttributeplaced on a string property will by default produce a validation error for an empty string the same way it would for a null value.There are two easy ways you can change this:
``` C#
public class Person
{
public int Id { get; set; }
[Required(AllowEmptyStrings = true)]
public string Name { get; set; }
}
EF7 will not automatically perform validation on SaveChanges(). It will leave it to other frameworks or to the application to do it.