@@ -140,9 +140,12 @@ public virtual Column[] GetColumns(string table)
140140 public virtual ForeignKeyConstraint [ ] GetForeignKeyConstraints ( string table )
141141 {
142142 var constraints = new List < ForeignKeyConstraint > ( ) ;
143- using ( IDbCommand cmd = CreateCommand ( ) )
143+ using ( var cmd = CreateCommand ( ) )
144144 using (
145- IDataReader reader =
145+ var reader =
146+ // TODO:
147+ // In this statement the naming of alias PK is misleading since INFORMATION_SCHEMA.TABLE_CONSTRAINTS (alias PK) is the child
148+ // while INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS (alias C) is the parent
146149 ExecuteQuery (
147150 cmd , String . Format ( "SELECT K_Table = FK.TABLE_NAME, FK_Column = CU.COLUMN_NAME, PK_Table = PK.TABLE_NAME, PK_Column = PT.COLUMN_NAME, Constraint_Name = C.CONSTRAINT_NAME FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME INNER JOIN ( SELECT i1.TABLE_NAME, i2.COLUMN_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1 INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2 ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME WHERE i1.CONSTRAINT_TYPE = 'PRIMARY KEY' ) PT ON PT.TABLE_NAME = PK.TABLE_NAME WHERE FK.table_name = '{0}'" , table ) ) )
148151 {
@@ -151,10 +154,10 @@ public virtual ForeignKeyConstraint[] GetForeignKeyConstraints(string table)
151154 var constraint = new ForeignKeyConstraint
152155 {
153156 Name = reader . GetString ( 4 ) ,
154- Table = reader . GetString ( 0 ) ,
155- Columns = new [ ] { reader . GetString ( 1 ) } ,
156- PkTable = reader . GetString ( 2 ) ,
157- PkColumns = new [ ] { reader . GetString ( 3 ) }
157+ ParentTable = reader . GetString ( 0 ) ,
158+ ParentColumns = [ reader . GetString ( 1 ) ] ,
159+ ChildTable = reader . GetString ( 2 ) ,
160+ ChildColumns = [ reader . GetString ( 3 ) ]
158161 } ;
159162
160163 constraints . Add ( constraint ) ;
@@ -748,18 +751,18 @@ public virtual void GenerateForeignKey(string primaryTable, string[] primaryColu
748751
749752 public virtual void AddForeignKey ( string table , ForeignKeyConstraint fk )
750753 {
751- AddForeignKey ( fk . Name , table , fk . Columns , fk . PkTable , fk . PkColumns ) ;
754+ AddForeignKey ( fk . Name , table , fk . ParentColumns , fk . ChildTable , fk . ChildColumns ) ;
752755 }
753756
754- public virtual void AddForeignKey ( string name , string primaryTable , string primaryColumn , string refTable , string refColumn )
757+ public virtual void AddForeignKey ( string name , string parentTable , string parentColumn , string childTable , string childColumn )
755758 {
756759 try
757760 {
758- AddForeignKey ( name , primaryTable , new [ ] { primaryColumn } , refTable , new [ ] { refColumn } ) ;
761+ AddForeignKey ( name , parentTable , [ parentColumn ] , childTable , [ childColumn ] ) ;
759762 }
760763 catch ( Exception ex )
761764 {
762- throw new Exception ( string . Format ( "Error occured while adding foreign key: \" {0}\" between table: \" {1}\" and table: \" {2}\" - see inner exception for details" , name , primaryTable , refTable ) , ex ) ;
765+ throw new Exception ( string . Format ( "Error occured while adding foreign key: \" {0}\" between table: \" {1}\" and table: \" {2}\" - see inner exception for details" , name , parentTable , childTable ) , ex ) ;
763766 }
764767 }
765768
@@ -768,32 +771,32 @@ public virtual void AddForeignKey(string name, string primaryTable, string prima
768771 /// AddForeignKey(string, string, string, string, string)
769772 /// </see>
770773 /// </summary>
771- public virtual void AddForeignKey ( string name , string primaryTable , string [ ] primaryColumns , string refTable , string [ ] refColumns )
774+ public virtual void AddForeignKey ( string name , string parentTable , string [ ] parentColumns , string childTable , string [ ] childColumns )
772775 {
773- AddForeignKey ( name , primaryTable , primaryColumns , refTable , refColumns , ForeignKeyConstraintType . NoAction ) ;
776+ AddForeignKey ( name , parentTable , parentColumns , childTable , childColumns , ForeignKeyConstraintType . NoAction ) ;
774777 }
775778
776- public virtual void AddForeignKey ( string name , string primaryTable , string primaryColumn , string refTable , string refColumn , ForeignKeyConstraintType constraint )
779+ public virtual void AddForeignKey ( string name , string parentTable , string parentColumn , string childTable , string childColumn , ForeignKeyConstraintType constraint )
777780 {
778- AddForeignKey ( name , primaryTable , new [ ] { primaryColumn } , refTable , new [ ] { refColumn } ,
781+ AddForeignKey ( name , parentTable , new [ ] { parentColumn } , childTable , new [ ] { childColumn } ,
779782 constraint ) ;
780783 }
781784
782- public virtual void AddForeignKey ( string name , string primaryTable , string [ ] primaryColumns , string refTable ,
783- string [ ] refColumns , ForeignKeyConstraintType constraint )
785+ public virtual void AddForeignKey ( string name , string parentTable , string [ ] parentColumns , string childTable ,
786+ string [ ] childColumns , ForeignKeyConstraintType constraint )
784787 {
785- refTable = QuoteTableNameIfRequired ( refTable ) ;
786- primaryTable = QuoteTableNameIfRequired ( primaryTable ) ;
787- QuoteColumnNames ( primaryColumns ) ;
788- QuoteColumnNames ( refColumns ) ;
788+ childTable = QuoteTableNameIfRequired ( childTable ) ;
789+ parentTable = QuoteTableNameIfRequired ( parentTable ) ;
790+ QuoteColumnNames ( parentColumns ) ;
791+ QuoteColumnNames ( childColumns ) ;
789792
790793 string constraintResolved = constraintMapper . SqlForConstraint ( constraint ) ;
791794
792795 ExecuteNonQuery (
793796 String . Format (
794797 "ALTER TABLE {0} ADD CONSTRAINT {1} FOREIGN KEY ({2}) REFERENCES {3} ({4}) ON UPDATE {5} ON DELETE {6}" ,
795- primaryTable , name , String . Join ( "," , primaryColumns ) ,
796- refTable , String . Join ( "," , refColumns ) , constraintResolved , constraintResolved ) ) ;
798+ parentTable , name , String . Join ( "," , parentColumns ) ,
799+ childTable , String . Join ( "," , childColumns ) , constraintResolved , constraintResolved ) ) ;
797800 }
798801
799802 /// <summary>
0 commit comments