我应该在构造函数中初始化集合

Omu*_*Omu 4 entity-framework-4

我和many-to-many之间有关系FooBar

这是一个简化的Foo

public class Foo
{
    public virtual ICollection<Bar> Bars {get;set;}
}
Run Code Online (Sandbox Code Playgroud)

我想保存一个附带一些条形的新foo:

var foo = new Foo();
foo.Bars.Add(bar1); //I'll get an error that if Bars is not initialized
repo.Insert(foo);
repo.SaveChanges();
Run Code Online (Sandbox Code Playgroud)

我应该在构造函数中初始化Bars,这应该怎么做?(使用EF4 CTP5 codefirst)

Rom*_*ain 5

我确实在构造函数中初始化集合(如果实际上总是需要集合),或者在首次需要集合时将getter更改为初始化(如果不总是需要集合).