Collections: It doesn't matter.
There is a clear distinction between collections and links as navigation properties. A link is an object. Collections contain objects. This means that initializing the collection is pointless from the point of view of business logic: it does not determine the relationship between entities. Link setting is in progress.
So this is solely a matter of preference, whether you are or not, or how you initialize inline lists.
As for the how, some people prefer lazy initialization:
private ICollection<Address> _addresses; public virtual ICollection<Address> Addresses { get { return this._addresses ?? (this._addresses = new HashSet<Address>()); }
It prevents unnecessary reference exceptions, so it facilitates unit testing and assembly control, but also prevents unnecessary initialization. The latter can make a difference when a class has relatively many collections. The disadvantage is that relatively much plumbing is required, especially. Compared to auto properties without initialization. In addition, the emergence of the null distribution operator in C # made initialization of collection properties less relevant.
... if explicit loading is not applied
The only thing that initializing collections makes it difficult to verify if the assembly has been loaded by the Entity Framework. If the collection is initialized, a statement like ...
var users = context.Users.ToList();
... will create User objects that have empty, but not null Addresses collections (deferred loading). Checking if a collection is loaded requires code ...
var user = users.First(); var isLoaded = context.Entry(user).Collection(c => c.Addresses).IsLoaded;
If the collection is not initialized, a simple null check will be performed. Therefore, when selective explicit loading is an important part of your coding practice, that is ...
if () context.Entry(user).Collection(c => c.Addresses).Load();
... it may be more convenient not to initialize the properties of the collection.
Initial Properties: Not
Reference properties are objects, so assigning an empty object to them makes sense .
Even worse, if you initiate them in the constructor, EF will not overwrite them when materializing your object or lazy loading. They will always have their initial values ββuntil you replace them. What's worse, you can even save empty objects in the database!
And there is another effect: the correction of relations will not occur. Link fixing is the process by which EF connects all objects in a context using its navigation properties. When User and Licence are loaded separately, still User.License will be filled in and vice versa. Unless, of course, if License was not initialized in the constructor. This is true for 1: n associations. If Address will initialize User in its constructor, User.Addresses will not be populated!
Entity Framework Kernel
Fixing relations in the kernel of the Entity Framework (2.1 at the time of writing) does not affect the initialized properties of link navigation in constructors. That is, when users and addresses are retrieved from the database separately, the navigation properties are populated.
However, lazy loading does not overwrite the initialized navigation links of the property. So, in conclusion, as well as in the EF kernel, initializes the links of navigation properties in the constructors can cause problems. Do not do this. In any case, it doesnβt make sense,