1

I'm using EF6 Code First, I have a sample class

public class Department
{
    public Department()
    {
        Workers = new List<Employee>();
    }

    public int Id { get; set; }
    public virtual List<Employee> Workers { get; set; }
}

Do I really need that horrible constructor there?

I mean, the collection will not construct itself by magic, but I write code in such a way that all EF entities are always proxies (using Create() on DbSets when I need a new object). So I first expected for those "lazy" properties just to initialize themselves. After all, that's what proxies are for, I do these kind of things with Castle.Proxy, for example. But it seems that this functionality is not included in EF, or am I wrong?

I know I can write my own monstrous-looking property for lazy initialization of this collection, and then copy it everywhere I go, but I might as well be stuck with putting it all in the constructor. My goal is for my POCOs to look Plain.

Maybe there is a way to write some custom interceptor that will first initialize the collection if it is null?

evilkos
  • 545
  • 10
  • 17
  • Have a look at this discussion http://stackoverflow.com/questions/20757594/ef-codefirst-should-i-initialize-navigation-properties – footyapps27 Jan 10 '17 at 09:52

0 Answers0