Entity Framework default value for null properties on select

asp.net-web-api2 c# entity-framework entity-framework-6

Question

I am possibly looking at moving from nHibernate for my ORM to entity framework and I am running into a small issue if the database is has a null value, but the entity property is not nullable. nHibernate will just set the default value and move on and will not cause any exceptions. Meaning if I have a boolean property it will be false if the database is null.

In entity framework (6) it throws an exception. Is there some configuration setting that I am missing to tell EF to set a default value if the property is not nullable and the database value is null?

1
0
7/13/2016 5:22:27 PM

Accepted Answer

Your entity properties don't have to be automatic properties, so you could, for instance do:

public class SomeEntity
{
  private bool _field
  public bool? Field
  {
    get { return _field; }
    set { _field = value.HasValue ? false : value.Value; }
  }
}
1
7/13/2016 5:27:38 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow