实体框架4.1代码第一 - 如果一对多的关系ICollections被初始化 [英] Entity Framework 4.1 Code First - Should many relationship ICollections be initialised

查看:118
本文介绍了实体框架4.1代码第一 - 如果一对多的关系ICollections被初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在实体框架4.1创建POCO时应类进行编码以初始化许多关系或者是有一些理由让实体框架具有对这些特性的控制?

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?

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

public class Portfolio 
{
    public virtual ICollection<Visit> Visits
    {
        get;
        set;
    }
}



有没有更好的格局依然?

Is there a better pattern still?

推荐答案

第一个版本是正确的。它可以让你来初始化集合,当你创建一个新的实体,而是在同一时间,它可以让EF来时,它物化从数据库加载实体,由懒加载动态代理wrapps它初始化集合。

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.

这篇关于实体框架4.1代码第一 - 如果一对多的关系ICollections被初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆