@@ -27,29 +27,26 @@ public class ForeignKey : Constraint
27
27
/// <returns>
28
28
/// A string that contains the SQL to create the named Foreign Key Constraint.
29
29
/// </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 ) ;
45
49
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 ) ;
53
50
return cascadeDeleteEnabled && d . SupportsCascadeDelete ? result + " on delete cascade" : result ;
54
51
}
55
52
@@ -217,10 +214,7 @@ public string ReferencedEntityName
217
214
}
218
215
219
216
/// <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 ;
224
218
225
219
public string GeneratedConstraintNamePrefix => "FK_" ;
226
220
@@ -231,12 +225,7 @@ public override bool IsGenerated(Dialect.Dialect dialect)
231
225
if ( dialect . SupportsNullInUnique || IsReferenceToPrimaryKey )
232
226
return true ;
233
227
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 ) ;
240
229
}
241
230
}
242
231
}
0 commit comments