public override void IdentifyForeignKeys(List <ForeignKey> fkList, Tables tables) { foreach (var foreignKey in fkList) { Table fkTable = tables.GetTable(foreignKey.FkTableName, foreignKey.FkSchema); if (fkTable == null) { continue; // Could be filtered out } Table pkTable = tables.GetTable(foreignKey.PkTableName, foreignKey.PkSchema); if (pkTable == null) { continue; // Could be filtered out } Column fkCol = fkTable.Columns.Find(n => n.PropertyName == foreignKey.FkColumn); if (fkCol == null) { continue; // Could not find fk column } Column pkCol = pkTable.Columns.Find(n => n.PropertyName == foreignKey.PkColumn); if (pkCol == null) { continue; // Could not find pk column } fkTable.HasForeignKey = true; } }
public override void ProcessForeignKeys(List <ForeignKey> fkList, Tables tables, bool useCamelCase, bool prependSchemaName, string collectionType, bool checkForFkNameClashes, bool includeComments) { var constraints = fkList.Select(x => x.ConstraintName).Distinct(); foreach (var constraint in constraints) { var localConstraint = constraint; var foreignKeys = fkList.Where(x => x.ConstraintName == localConstraint).ToList(); var casete = fkList.Where(x => x.ConstraintName == localConstraint).Select(t => t.Cascade).First(); var foreignKey = foreignKeys.First(); Table fkTable = tables.GetTable(foreignKey.FkTableName, foreignKey.FkSchema); if (fkTable == null || fkTable.IsMapping || !fkTable.HasForeignKey) { continue; } Table pkTable = tables.GetTable(foreignKey.PkTableName, foreignKey.PkSchema); if (pkTable == null || pkTable.IsMapping) { continue; } var fkCols = foreignKeys.Select(x => new { fkOrdinal = x.Ordinal, col = fkTable.Columns.Find(n => n.PropertyName == x.FkColumn || n.PropertyName == x.FkColumn + "_") }) .Where(x => x != null) .ToList(); var pkCols = foreignKeys.Select(x => pkTable.Columns.Find(n => n.PropertyName == x.PkColumn)).Where(x => x != null).OrderBy(o => o.Ordinal).ToList(); if (!pkCols.Any()) { pkCols = foreignKeys.Select(x => pkTable.Columns.Find(n => n.PropertyName == x.PkColumn + "_")).Where(x => x != null).OrderBy(o => o.Ordinal).ToList(); } var fkCol = fkCols.First(); var pkCol = pkCols.First(); if (!pkCol.IsPrimaryKey) { continue; } var relationship = CodeFirstTools.CalcRelationship(pkTable, fkTable, fkCol.col, pkCol); string pkTableHumanCase = foreignKey.PkTableHumanCase(useCamelCase, prependSchemaName); string pkPropName = fkTable.GetUniqueColumnPropertyName(pkTableHumanCase, foreignKey, useCamelCase, checkForFkNameClashes, true); bool fkMakePropNameSingular = (relationship == Relationship.OneToOne); string fkPropName = pkTable.GetUniqueColumnPropertyName(fkTable.NameHumanCase, foreignKey, useCamelCase, checkForFkNameClashes, fkMakePropNameSingular); fkCol.col.EntityFk = string.Format("public virtual {0} {1} {2}{3}", pkTable.Name, pkPropName, "{ get; set; }", includeComments ? " // " + foreignKey.ConstraintName : string.Empty); string manyToManyMapping; if (foreignKeys.Count > 1) { manyToManyMapping = string.Format("c => new {{ {0} }}", string.Join(", ", fkCols.OrderBy(o => o.fkOrdinal).Select(x => "c." + x.col.PropertyName).ToArray())); } else { manyToManyMapping = string.Format("c => c.{0}", fkCol.col.PropertyName); } fkCol.col.ConfigFk = string.Format(" {0};{1}", GetRelationship(relationship, fkCol.col, pkCol, pkPropName, fkPropName, manyToManyMapping, casete), includeComments ? " // " + foreignKey.ConstraintName : string.Empty); pkTable.AddReverseNavigation(relationship, pkTableHumanCase, fkTable, fkPropName, string.Format("{0}.{1}", fkTable.Name, foreignKey.ConstraintName), collectionType, includeComments); } }
public void IdentifyMappingTable(List <ForeignKey> fkList, Tables tables, bool useCamelCase, string collectionType, bool checkForFkNameClashes, bool includeComments, bool isSqlCE) { IsMapping = false; // Must have only 2 columns to be a mapping table if (Columns.Count != 2) { return; } // All columns must be primary keys if (PrimaryKeys.Count() != 2) { return; } // No columns should be nullable if (Columns.Any(x => x.IsNullable)) { return; } // Find the foreign keys for this table var foreignKeys = fkList.Where(x => String.Compare(x.FkTableName, Name, StringComparison.OrdinalIgnoreCase) == 0 && String.Compare(x.FkSchema, Schema, StringComparison.OrdinalIgnoreCase) == 0) .ToList(); // Each column must have a foreign key, therefore check column and foreign key counts match if (foreignKeys.Select(x => x.FkColumn).Distinct().Count() != 2) { return; } ForeignKey left = foreignKeys[0]; ForeignKey right = foreignKeys[1]; Table leftTable = tables.GetTable(left.PkTableName, left.PkSchema); if (leftTable == null) { return; } Table rightTable = tables.GetTable(right.PkTableName, right.PkSchema); if (rightTable == null) { return; } if (leftTable == rightTable) { return; } var leftPropName = leftTable.GetUniqueColumnPropertyName(rightTable.NameHumanCase, right, useCamelCase, checkForFkNameClashes, false); var rightPropName = rightTable.GetUniqueColumnPropertyName(leftTable.NameHumanCase, left, useCamelCase, checkForFkNameClashes, false); leftTable.AddMappingConfiguration(left, right, useCamelCase, leftPropName, rightPropName, isSqlCE); IsMapping = true; rightTable.AddReverseNavigation(Relationship.ManyToMany, rightTable.NameHumanCase, leftTable, rightPropName, null, collectionType, includeComments); leftTable.AddReverseNavigation(Relationship.ManyToMany, leftTable.NameHumanCase, rightTable, leftPropName, null, collectionType, includeComments); }