private void EnsureGeneratedFKsCollectionIsInitialized() { if (this.generatedFKs != null) return; this.generatedFKs = new ForeignKeyCollection(); foreach (ForeignKey fk in this.foreignKeys) { // Skip FKs to tables for which code is not generated. if (IsTableGenerated(this.generatedTables, fk.ParentTableName)) this.generatedFKs.Add(fk); } }
private static Dictionary<string, ForeignKey> FindParallelFKs(ForeignKeyCollection generatedFKs) { // Find parallel FKs (multiple FKs to the same table). They need additional name resolving. // Example: Employee table has ReportsTo (employee manager) and ControlledBy (employee quality assurance controller) columns. // There are two recursive relations in the table. Lets say that there are 3 levels. // 1. ReportsTo branch: Worker-Manager. // 2. ControlledBy branch: Worker-Controller. // Generate FK members with the names: // 1. Worker_ReportsTo. // 2. Worker_ControlledBy. Dictionary<string, ForeignKey> parallelFKs = new Dictionary<string, ForeignKey>(); for (int i=0; i<generatedFKs.Count; i++) { // Find parallel relations. for (int j=i+1; j<generatedFKs.Count; j++) { if (generatedFKs[i].ParentTableName == generatedFKs[j].ParentTableName) { // Both paralel relations will use aliases for parent tables. if (!parallelFKs.ContainsKey(generatedFKs[i].ConstraintName)) parallelFKs[generatedFKs[i].ConstraintName] = generatedFKs[i]; if (!parallelFKs.ContainsKey(generatedFKs[j].ConstraintName)) parallelFKs[generatedFKs[j].ConstraintName] = generatedFKs[j]; } } } return parallelFKs; }
/// <summary>Gets parallel FKs, ie. FKs which reference the same parent table. These need additional name resolving.</summary> public ForeignKeyCollection FindParallelFKs() { ForeignKeyCollection parallelFKs = new ForeignKeyCollection(); foreach (KeyValuePair<string, ForeignKey> nameAndFk in FindParallelFKs(this)) parallelFKs.list.Add(nameAndFk.Value); return parallelFKs; }