@@ -3265,9 +3265,6 @@ SELECT SERVERPROPERTY('Edition') AS Edition,
32653265 fk.fk.IncludeRequiredAttribute = true;
32663266 }
32673267
3268- var fkCol = fkCols.First();
3269- var pkCol = pkCols.First();
3270-
32713268 foreignKey = Settings.ForeignKeyProcessing(foreignKeys, fkTable, pkTable, fkCols.Any(x => x.col.IsNullable));
32723269
32733270 string pkTableHumanCaseWithSuffix = foreignKey.PkTableHumanCase(pkTable.Suffix);
@@ -3313,23 +3310,27 @@ SELECT SERVERPROPERTY('Edition') AS Edition,
33133310 string.Join(", ", fkCols.Select(x => "[" + x.col.NameHumanCase + "]").Distinct().ToArray()),
33143311 foreignKey.ConstraintName)
33153312 };
3316- fkCol.col.EntityFk.Add(fkd);
3313+
3314+ var firstFKCol = fkCols.First();
3315+ firstFKCol.col.EntityFk.Add(fkd);
33173316
33183317 string manyToManyMapping, mapKey;
33193318 if(foreignKeys.Count > 1)
33203319 {
33213320 manyToManyMapping = string.Format("c => new {{ {0} }}", string.Join(", ", fkCols.Select(x => "c." + x.col.NameHumanCase).Distinct().ToArray()));
33223321 mapKey = string.Format("{0}", string.Join(",", fkCols.Select(x => "\"" + x.col.Name + "\"").Distinct().ToArray()));
33233322 } else {
3324- manyToManyMapping = string.Format("c => c.{0}", fkCol .col.NameHumanCase);
3325- mapKey = string.Format("\"{0}\"", fkCol .col.Name);
3323+ manyToManyMapping = string.Format("c => c.{0}", firstFKCol .col.NameHumanCase);
3324+ mapKey = string.Format("\"{0}\"", firstFKCol .col.Name);
33263325 }
33273326
33283327 if (!Settings.UseDataAnnotations)
33293328 {
3330- string rel = GetRelationship(relationship, fkCol.col, pkCol, pkPropName, fkPropName, manyToManyMapping, mapKey, foreignKey.CascadeOnDelete, foreignKey.IncludeReverseNavigation, foreignKey.IsNotEnforced);
3329+ List<Column> fkCols2 = fkCols.Select( c => c.col ).ToList();
3330+
3331+ string rel = GetRelationship(relationship, fkCols2, pkCols, pkPropName, fkPropName, manyToManyMapping, mapKey, foreignKey.CascadeOnDelete, foreignKey.IncludeReverseNavigation, foreignKey.IsNotEnforced);
33313332 string com = Settings.IncludeComments != CommentsStyle.None ? " // " + foreignKey.ConstraintName : string.Empty;
3332- fkCol .col.ConfigFk.Add(string.Format("{0};{1}", rel, com));
3333+ firstFKCol .col.ConfigFk.Add(string.Format("{0};{1}", rel, com));
33333334 }
33343335
33353336 if(foreignKey.IncludeReverseNavigation)
@@ -3363,41 +3364,51 @@ SELECT SERVERPROPERTY('Edition') AS Edition,
33633364 }
33643365 }
33653366
3366- private static string GetRelationship(Relationship relationship, Column fkCol, Column pkCol , string pkPropName, string fkPropName, string manyToManyMapping, string mapKey, bool cascadeOnDelete, bool includeReverseNavigation, bool isNotEnforced)
3367+ private static string GetRelationship(Relationship relationship, IList< Column> fkCols, IList< Column> pkCols , string pkPropName, string fkPropName, string manyToManyMapping, string mapKey, bool cascadeOnDelete, bool includeReverseNavigation, bool isNotEnforced)
33673368 {
3368- return string.Format("Has{0}(a => a.{1}){2}{3}",
3369- GetHasMethod(relationship, fkCol, pkCol, isNotEnforced),
3369+ string hasMethod = GetHasMethod(relationship, fkCols, pkCols, isNotEnforced);
3370+ string withMethod = GetWithMethod(relationship, fkCols, fkPropName, manyToManyMapping, mapKey, includeReverseNavigation);
3371+
3372+ return string.Format("{0}(a => a.{1}){2}{3}",
3373+ hasMethod,
33703374 pkPropName,
3371- GetWithMethod(relationship, fkCol, fkPropName, manyToManyMapping, mapKey, includeReverseNavigation) ,
3375+ withMethod ,
33723376 cascadeOnDelete ? string.Empty: ".WillCascadeOnDelete(false)");
33733377 }
33743378
33753379 // HasOptional
33763380 // HasRequired
33773381 // HasMany
3378- private static string GetHasMethod(Relationship relationship, Column fkCol, Column pkCol , bool isNotEnforced)
3382+ private static string GetHasMethod(Relationship relationship, IList< Column> fkCols, IList< Column> pkCols , bool isNotEnforced)
33793383 {
3380- bool withMany = false;
3381- switch (relationship)
3382- {
3383- case Relationship.ManyToOne:
3384- case Relationship.ManyToMany:
3385- withMany = true;
3386- break;
3387- }
3388-
3389- if (withMany || pkCol.IsPrimaryKey || pkCol.IsUniqueConstraint || pkCol.IsUnique)
3390- return fkCol.IsNullable || isNotEnforced ? "Optional" : "Required";
3384+ bool withMany = ( relationship == Relationship.ManyToOne || relationship == Relationship.ManyToMany );
3385+ bool fkIsNullable = fkCols.Any( c => c.IsNullable );
3386+ bool pkIsUnique = pkCols.Any( c => c.IsUnique || c.IsUniqueConstraint || c.IsPrimaryKey );
3387+
3388+ if ( withMany || pkIsUnique )
3389+ {
3390+ if( fkIsNullable || isNotEnforced )
3391+ {
3392+ return "HasOptional";
3393+ }
3394+ else
3395+ {
3396+ return "HasRequired";
3397+ }
3398+ }
3399+ else
3400+ {
3401+ return "HasMany";
3402+ }
33913403
3392- return "Many";
33933404 }
33943405
33953406 // WithOptional
33963407 // WithRequired
33973408 // WithMany
33983409 // WithRequiredPrincipal
33993410 // WithRequiredDependent
3400- private static string GetWithMethod(Relationship relationship, Column fkCol , string fkPropName, string manyToManyMapping, string mapKey, bool includeReverseNavigation)
3411+ private static string GetWithMethod(Relationship relationship, IList< Column> fkCols , string fkPropName, string manyToManyMapping, string mapKey, bool includeReverseNavigation)
34013412 {
34023413 string withParam = includeReverseNavigation ? string.Format("b => b.{0}", fkPropName) : string.Empty;
34033414 switch (relationship)
@@ -3409,7 +3420,7 @@ SELECT SERVERPROPERTY('Edition') AS Edition,
34093420 return string.Format(".WithRequiredDependent({0})", withParam);
34103421
34113422 case Relationship.ManyToOne:
3412- if (!fkCol. Hidden)
3423+ if (!fkCols.Any( c => c. Hidden ) )
34133424 return string.Format(".WithMany({0}).HasForeignKey({1})", withParam, manyToManyMapping); // Foreign Key Association
34143425 return string.Format(".WithMany({0}).Map(c => c.MapKey({1}))", withParam, mapKey); // Independent Association
34153426
0 commit comments