Background:
I have an entity that has a String property named Description.
In database it maps to the NOT NULL NVARCHAR(200)
.
Problem:
When I try to insert a new row of that entity, this is what I do:
MyExampleEntity entity = new MyExampleEntity()
{
Name = "example",
Description = "" // NOTE THIS LINE!
};
DatabaseContext db = new DatabaseContext();
db.MyExampleEntities.Add(entity);
db.SubmitChanges();
This, however, causes an exception saying "The Description field is required."
Question:
Should not the "empty string" be simply that - a string with zero characters?
I believe only Description = null
should be treated as providing no value.
Description to ""
and cause an exception when Description = null
(having no value)?This appears to be a symptom of Entity Framework.
Related Article
Some data annotations can be used to overcome this:
[MetadataType(typeof(Report_META))]
public partial class Report
{
}
public partial class Report_META
{
[Required(AllowEmptyStrings = true)]
[DisplayFormat(ConvertEmptyStringToNull = false)]
public object Note { get; set; }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With