简体   繁体   English

实体框架4.1代码优先 - 应初始化许多关系ICollections

[英]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.

相关问题 Entity Framework 4.1 - Code First:多对多关系 - Entity Framework 4.1 - Code First: many-to-many relationship 如何使用Entity Framework 4.1 Code First强制数据库中一对多关系的一对一关系 - How to use Entity Framework 4.1 Code First to force a one to one relationship for a one to many relationship in the database 实体框架4.1代码优先和一对多映射问题 - Entity Framework 4.1 Code First and One-to-Many mapping problem 首先在实体框架代码中映射多对多关系 - Mapping many to many relationship in entity framework code first 实体框架代码在单个表中的第一个多对多关系 - Entity Framework Code First Many to Many relationship(s) in single table 实体框架代码优先在同一个表上的多对多关系 - Entity Framework Code-first Many to many relationship on the same table 实体框架代码首先与现有实体进行多对多关系 - Entity Framework Code First Many to Many Relationship with Existing Entities 实体框架核心代码优先-0..1至多关系和级联删除 - Entity Framework Core code first - 0..1 to many relationship and cascade delete 无需代码优先模型创建的一对多关系实体框架 - One To Many relationship entity framework without Code First Model Creation Entity Framework 5.0代码首先是一对一和一对多的关系 - Entity Framework 5.0 code first one to one and one to many relationship
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM