You can get reference navigation properties from the Entity Framework conceptual model by this method:
IEnumerable<string> GetReferenceProperies<T>(DbContext context)
{
var oc = ((IObjectContextAdapter)context).ObjectContext;
var entityType = oc.MetadataWorkspace.GetItems(DataSpace.OSpace)
.OfType<EntityType>()
.FirstOrDefault (et => et.Name == typeof(T).Name);
if (entityType != null)
{
foreach (NavigationProperty np in entityType.NavigationProperties
.Where(p => p.ToEndMember.RelationshipMultiplicity
== RelationshipMultiplicity.One
|| p.ToEndMember.RelationshipMultiplicity
== RelationshipMultiplicity.ZeroOrOne))
{
yield return np.Name;
}
}
}
It gets all navigation properties that have 0..1
at the end of the association, so that excludes collection navigation properties.
Now you can use the property names to get the matching PropertyInfo
s and get the value of the properties.