That's what I did and what works for me with EF Database first.
That's what you need to be generated:
public partial class Parent
{
public Parent()
{
this.Children= new ObservableCollection<Child>();
}
So that default costructor will be replaced.
And ObservableCollection is ICollection, so you don't need to change anything else.
To make this appear every time you update your database model you have to change your .tt file with following parts:
public string UsingDirectives(bool inHeader, bool includeCollections = true)
{
return inHeader == string.IsNullOrEmpty(_code.VsNamespaceSuggestion())
? string.Format(
CultureInfo.InvariantCulture,
"{0}using System;{1}" +
"{2}",
inHeader ? Environment.NewLine : "",
includeCollections ? (Environment.NewLine + "using System.Collections.ObjectModel;"
+ Environment.NewLine + "using System.Collections.Generic;") : "",
inHeader ? "" : Environment.NewLine)
: "";
}
and this:
foreach (var navigationProperty in collectionNavigationProperties)
{
this.<#=code.Escape(navigationProperty)#> = new ObservableCollection<<#=typeMapper.GetTypeName(navigationProperty.ToEndMember.GetEntityType())#>>();
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…