简体   繁体   中英

Entity Framework default value for null properties on select

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?

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; }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM