public static List<EFBase> GetForeignKeyList(EFBase ancestor, PropertyInfo fkPropertyInfo) { IEnumerable col = null; string collectionPropertyName = fkPropertyInfo.Pluralize(); foreach (var pi in GetPrimaryKeys(ancestor.GetType()).Where(pi => pi.Name != "Id").Reverse()) { string ancestorPropertyName = pi.Name.StripEnd(); ancestor = ancestor.LoadReference(ancestorPropertyName); var colPropertyInfo = ancestor.GetType().GetProperty(collectionPropertyName); if (colPropertyInfo != null) { col = (IEnumerable)colPropertyInfo.GetValue(ancestor); if (col == null) col = ancestor.LoadCollection(collectionPropertyName); break; } } return col.Cast<EFBase>().ToList(); }
private Type GetParentType(EFBase parent) { // parent might be inside a dynamic proxy assembly; need to convert return typeof(EFBase).Assembly.GetType($"{typeof(EFBase).Namespace}.{parent.GetType().Name.StripEnd()}"); }