internal static IEnumerable <PropertyInfo> UnionNavPropsWithFKs (this IEnumerable <PropertyInfo> navProps, EntityTypeInfo typeInfo, Type attributeToFilter, Func <ForeignKeyProperty, bool> fkFilter, out List <ForeignKeyProperty> FKs) { FKs = typeInfo .GetForeignKeysWithAttribute(attributeToFilter) .Where(fkFilter ?? (fk => true)) .ToList(); //add attributed fks that may have been set on scalar properties navProps = navProps .Union(FKs.Select(fk => fk.NavigationProperty)) .OrderNavs(typeInfo); return(navProps); }
internal static IEnumerable <PropertyInfo> OrderNavs(this IEnumerable <PropertyInfo> props, EntityTypeInfo typeInfo) { //order them obeying explicit column order var columnFKs = typeInfo.ColumnAttributeFKs; return(props .OrderBy(p => p.IsDefined(Defaults.ColumnType) ? (p.GetCustomAttribute(Defaults.ColumnType) as ColumnAttribute).Order : columnFKs.Where(fk => fk.NavigationProperty == p && fk.ScalarProperty?.IsDefined(Defaults.ColumnType) == true) .Select(fk => (int?)(fk.ScalarProperty.GetCustomAttribute(Defaults.ColumnType) as ColumnAttribute) .Order) .FirstOrDefault() ?? 0) //first order by explicit column attribute .ThenBy(p => typeInfo.AllProperties.IndexOf(p)) //then by how they appear on the class. ); }