How I can check in T4 template whether a navigation property is at principal end
I am tweaking T4 template to customize my autogeneration. As part of this I need to find a way whether a given navigation property of an entity is at principal end.
For example, if we have two entites, say Customer and Phone and I have a 1 to many relation from Customer to 开发者_JAVA百科Phone. Then, I need to check whether Customer entity is at principal end for the relation where navigation property "Phones" is participating.
What is the corresponding T4 template functions to do this or how to set up this condition? Please help.
It depends if you are using independent realtions or foreign key relations.
For independent relations you can find principal end of 1:N by checking multiplicity. I would try something like:
// check each navigation property
foreach (var navProperty in entity.NavigationProperties)
{
// use only properties where one end has * an second end has 1 or 0..1 multiplicity
// Not sure if the condition should not be reversed
if(navProperty.ToEndMember.RelaltionshipMultiplicity ==
RelationshipMultiplicity.Many &&
navProperty.FromEndMember.RelationshipMultiplicity !=
RelationshipMultiplicity.Many)
{
...
}
}
In case of foreign key relationships you can use methods of MetadataTools
which is class included from EF.Utility.CS.ttinclude. Normally installed at:
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes
One of the method is GetPrincipalProperties(navProperty)
Both approaches are already used in POCO T4 template.
精彩评论