If you want to use the Entity Frameworks metadata, you need to go looking through the MetadataWorkspace
which hangs off the ObjectContext
.
The starting point is to get the EntityType for your base type, in your case Contact.
I have an EF tips series and in Tip 13 I show an extension method on MetadataWorkspace
that gets the EntityType
for a particular CLR Type:
public static EntityType GetCSpaceEntityType<T>(
this MetadataWorkspace workspace);
You can use this like this:
var contactEntity = ctx.MetadataWorkspace.GetCSpaceEntityType<Contact>();
Once you have this you can look at it's NavigationProperties to find the relationship's and the names you are interested in including:
i.e.
foreach(var np in contactEntity.NavigationProperties)
{
Console.WriteLine("Include: {0}", np.Name);
Console.WriteLine("... Recursively include ");
EntityType relatedType =
(np.ToEndMember.TypeUsage.EdmType as RefType).ElementType;
//TODO: go repeat the same process... i.e. look at the relatedTypes
// navProps too until you decide to stop.
}
Of course how you decide what you want to Include is up to you.
Hope this helps
Alex