static public PropertyInfo FindRefFieldInForeinEntity(Type whatFindType, Type whereFindType, Type reationTypeToFind, string nameToFind) { IEnumerable <PropertyInfo> whereEntityRefFields = AttrHelper.GetProperties(reationTypeToFind, whereFindType); PropertyInfo whereEntityRefField = null; if (nameToFind == null) { //nameToFind Can be null, then it is assumed that the foreinEntityRefField is the only one and matches the type (previously: by name with pk) foreach (var p in whereEntityRefFields) { Type refType = (typeof(JManyToOne).Equals(reationTypeToFind) || typeof(JOneToOne).Equals(reationTypeToFind)) ? p.PropertyType : AttrHelper.GetGenericListArgType(p.PropertyType); if (refType.Equals(whatFindType)) { if (whereEntityRefField != null) { throw new Exception("Found more than one field referenced to this entity type"); } whereEntityRefField = p; } } } else { //nameToFind Set explicitly, which is relevant if the table has several fields of the same type that refer to the same table foreach (var p in whereEntityRefFields) { Type refType = (typeof(JManyToOne).Equals(reationTypeToFind) || typeof(JOneToOne).Equals(reationTypeToFind)) ? p.PropertyType : AttrHelper.GetGenericListArgType(p.PropertyType); if (refType.Equals(whatFindType)) { if ( ((typeof(JManyToOne).Equals(reationTypeToFind) || typeof(JOneToMany).Equals(reationTypeToFind)) && (p.Name).Equals(nameToFind)) || (typeof(JManyToMany).Equals(reationTypeToFind) && (p.Name).Equals(nameToFind)) || (typeof(JOneToOne).Equals(reationTypeToFind) && (p.Name).Equals(nameToFind)) )//todo { whereEntityRefField = p; break; } } } } return(whereEntityRefField); }