[英]Entity Framework 4.1 Code First - Should many relationship ICollections be initialised
In Entity Framework 4.1 when creating a POCO, should the class be coded to initialise the Many relationships or is there some reason to allow the Entity Framework to have control over these properties? 在Entity Framework 4.1中创建POCO时,是否应该对类进行编码以初始化Many关系,或者是否有某些理由允许Entity Framework控制这些属性?
public class Portfolio
{
private ICollection<Visit> _visits;
public virtual ICollection<Visit> Visits
{
get
{
if (_visits == null)
{
_visits = new List<Visit>();
}
return _visits;
}
set
{
_visits = value;
}
}
}
Or 要么
public class Portfolio
{
public virtual ICollection<Visit> Visits
{
get;
set;
}
}
Is there a better pattern still? 还有更好的模式吗?
The first version is correct. 第一个版本是正确的。 It will allow you to initialize collection when you are creating a new entity but in the same time it will allow EF to initialize the collection when it materializes the entity loaded from DB and wrapps it by dynamic proxy for lazy loading. 它允许您在创建新实体时初始化集合,但同时它允许EF在实现从DB加载的实体时初始化集合,并通过动态代理对其进行包装以进行延迟加载。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.