I have default constraint on my caption field in database. In web app if caption is empty NULL value passed to database and database returns exception because samo field it cannot be null (Caption varchar(50) NOT NULL).
So, what is easiest way to set some value ("" - empty string) or avoid to send NULL to database if value is really not provided. How I can set that in EntityFrameowrk, or maybe in data access layer...?
Just define property with empty string right after new entity creation. For example, within constructor. Example
Before update existing entity, you can check property for null:
entity.StringProperty = entity.StringProperty ?? String.Empty;
_db.SaveChanges();
On my system MVC4 entity framework 5.0 I am using like this, (DataAnnotation)
[DefaultValue = DateTime.Now]
public DateTime MyDateTime { get; set; }
}
Or like this for others Types... string.Empty
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