[英]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. 我可能正在考虑从适用于ORM的nHibernate迁移到实体框架,如果数据库具有null值,但是实体属性不可为空,那么我将遇到一个小问题。 nHibernate will just set the default value and move on and will not cause any exceptions. nHibernate只会设置默认值并继续运行,不会引起任何异常。 Meaning if I have a boolean property it will be false if the database is null. 意思是如果我有一个布尔属性,如果数据库为空,它将为false。
In entity framework (6) it throws an exception. 在实体框架(6)中,它引发异常。 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? 如果该属性不可为空且数据库值为null,是否缺少一些配置设置来告诉EF设置默认值?
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; }
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.