protected static void AttachCyclicNavigationProperty(IObjectContextAdapter context, object parent, object child, PropertyInfo navProperty = null) { if (parent == null || child == null) { return; } var parentType = ObjectContext.GetObjectType(parent.GetType()); var childType = ObjectContext.GetObjectType(child.GetType()); var navigationProperties = context.GetNavigationPropertiesForType(childType); PropertyInfo parentNavigationProperty = null; if (navProperty != null) { parentNavigationProperty = navProperty; } else { parentNavigationProperty = navigationProperties .Where(navigation => navigation.TypeUsage.EdmType.Name == parentType.Name) .Select(navigation => childType.GetProperty(navigation.Name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)) .FirstOrDefault(); } if (parentNavigationProperty != null) { parentNavigationProperty.SetValue(child, parent, null); } }
internal static IEnumerable <NavigationProperty> GetRequiredNavigationPropertiesForType(this IObjectContextAdapter context, Type entityType) { return(context.GetNavigationPropertiesForType(ObjectContext.GetObjectType(entityType)) .Where(navigationProperty => navigationProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.One)); }