Thanks to Robert Harvey, I found the following solution: First, you have to add some stuff to your data model template MyModeName.tt
, which you can find under the MyModelName.edmx
in your project. I added a new partial method private partial void OnConstructorCompletion();
and called it at the end of the constructor. I modified the document as follows (lines 27-67):
<#=codeStringGenerator.EntityClassOpening(entity)#>
{
private partial void OnConstructorCompletion();
<#
var propertiesWithDefaultValues = typeMapper.GetPropertiesWithDefaultValues(entity);
var collectionNavigationProperties = typeMapper.GetCollectionNavigationProperties(entity);
var complexProperties = typeMapper.GetComplexProperties(entity);
if (propertiesWithDefaultValues.Any() || collectionNavigationProperties.Any() || complexProperties.Any())
{
#>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public <#=code.Escape(entity)#>()
{
<#
foreach (var edmProperty in propertiesWithDefaultValues)
{
#>
this.<#=code.Escape(edmProperty)#> = <#=typeMapper.CreateLiteral(edmProperty.DefaultValue)#>;
<#
}
foreach (var navigationProperty in collectionNavigationProperties)
{
#>
this.<#=code.Escape(navigationProperty)#> = new HashSet<<#=typeMapper.GetTypeName(navigationProperty.ToEndMember.GetEntityType())#>>();
<#
}
foreach (var complexProperty in complexProperties)
{
#>
this.<#=code.Escape(complexProperty)#> = new <#=typeMapper.GetTypeName(complexProperty.TypeUsage)#>();
<#
}
#>
OnConstructorCompletion();
}
Next, I implemented the method where required inside of my entity class:
public partial class MyEntity
{
public Lazy<ICollection<MyNavigationEntity>> NavigationProperty;
private partial void OnConstructorCompletion()
{
NavigationProperty= new Lazy<ICollection<MyNavigationEntity>>(() =>
{
DatabaseEntities db = (DatabaseEntities)EntityContextExtensions.GetDbContextFromEntity(this);
return db.MyNavigationEntity
.Where(ne => ne.ID == this.ID_MyNavigationEntity)
.ToHashSet();
});
}
}