@@ -27,29 +27,26 @@ public class ForeignKey : Constraint
2727 /// <returns>
2828 /// A string that contains the SQL to create the named Foreign Key Constraint.
2929 /// </returns>
30- public override string SqlConstraintString ( Dialect . Dialect d , string constraintName , string defaultCatalog , string defaultSchema )
31- {
32- string [ ] cols = new string [ ColumnSpan ] ;
33- string [ ] refcols = new string [ ColumnSpan ] ;
34- int i = 0 ;
35- IEnumerable < Column > refiter ;
36- if ( IsReferenceToPrimaryKey )
37- refiter = referencedTable . PrimaryKey . ColumnIterator ;
38- else
39- refiter = referencedColumns ?? Enumerable . Empty < Column > ( ) ;
40- foreach ( Column column in ColumnIterator )
41- {
42- cols [ i ] = column . GetQuotedName ( d ) ;
43- i ++ ;
44- }
30+ public override string SqlConstraintString (
31+ Dialect . Dialect d ,
32+ string constraintName ,
33+ string defaultCatalog ,
34+ string defaultSchema )
35+ {
36+ var refiter = IsReferenceToPrimaryKey
37+ ? referencedTable . PrimaryKey . Columns
38+ : referencedColumns ;
39+
40+ var cols = Columns . ToArray ( column => column . GetQuotedName ( d ) ) ;
41+ var refcols = refiter . ToArray ( column => column . GetQuotedName ( d ) ) ;
42+
43+ string result = d . GetAddForeignKeyConstraintString (
44+ constraintName ,
45+ cols ,
46+ referencedTable . GetQualifiedName ( d , defaultCatalog , defaultSchema ) ,
47+ refcols ,
48+ IsReferenceToPrimaryKey ) ;
4549
46- i = 0 ;
47- foreach ( Column column in refiter )
48- {
49- refcols [ i ] = column . GetQuotedName ( d ) ;
50- i ++ ;
51- }
52- string result = d . GetAddForeignKeyConstraintString ( constraintName , cols , referencedTable . GetQualifiedName ( d , defaultCatalog , defaultSchema ) , refcols , IsReferenceToPrimaryKey ) ;
5350 return cascadeDeleteEnabled && d . SupportsCascadeDelete ? result + " on delete cascade" : result ;
5451 }
5552
@@ -217,10 +214,7 @@ public string ReferencedEntityName
217214 }
218215
219216 /// <summary>Does this foreignkey reference the primary key of the reference table </summary>
220- public bool IsReferenceToPrimaryKey
221- {
222- get { return referencedColumns == null || referencedColumns . Count == 0 ; }
223- }
217+ public bool IsReferenceToPrimaryKey => referencedColumns == null || referencedColumns . Count == 0 ;
224218
225219 public string GeneratedConstraintNamePrefix => "FK_" ;
226220
@@ -231,12 +225,7 @@ public override bool IsGenerated(Dialect.Dialect dialect)
231225 if ( dialect . SupportsNullInUnique || IsReferenceToPrimaryKey )
232226 return true ;
233227
234- foreach ( var column in referencedColumns )
235- {
236- if ( column . IsNullable )
237- return false ;
238- }
239- return true ;
228+ return referencedColumns . All ( column => ! column . IsNullable ) ;
240229 }
241230 }
242231}
0 commit comments