Skip to content

Commit 9713ae3

Browse files
committed
GetHashMethod and GetWithMethod return the correct method names for composite keys.
1 parent b944365 commit 9713ae3

File tree

1 file changed

+36
-25
lines changed

1 file changed

+36
-25
lines changed

EntityFramework.Reverse.POCO.Generator/EF.Reverse.POCO.Core.ttinclude

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3264,9 +3264,6 @@ SELECT SERVERPROPERTY('Edition') AS Edition,
32643264
fk.fk.IncludeRequiredAttribute = true;
32653265
}
32663266

3267-
var fkCol = fkCols.First();
3268-
var pkCol = pkCols.First();
3269-
32703267
foreignKey = Settings.ForeignKeyProcessing(foreignKeys, fkTable, pkTable, fkCols.Any(x => x.col.IsNullable));
32713268

32723269
string pkTableHumanCaseWithSuffix = foreignKey.PkTableHumanCase(pkTable.Suffix);
@@ -3312,7 +3309,9 @@ SELECT SERVERPROPERTY('Edition') AS Edition,
33123309
string.Join(", ", fkCols.Select(x => "[" + x.col.NameHumanCase + "]").Distinct().ToArray()),
33133310
foreignKey.ConstraintName)
33143311
};
3315-
fkCol.col.EntityFk.Add(fkd);
3312+
3313+
var firstFKCol = fkCols.First();
3314+
firstFKCol.col.EntityFk.Add(fkd);
33163315

33173316
string manyToManyMapping, mapKey;
33183317
if(foreignKeys.Count > 1)
@@ -3326,9 +3325,11 @@ SELECT SERVERPROPERTY('Edition') AS Edition,
33263325

33273326
if (!Settings.UseDataAnnotations)
33283327
{
3329-
string rel = GetRelationship(relationship, fkCol.col, pkCol, pkPropName, fkPropName, manyToManyMapping, mapKey, foreignKey.CascadeOnDelete, foreignKey.IncludeReverseNavigation, foreignKey.IsNotEnforced);
3328+
List<Column> fkCols2 = fkCols.Select( c => c.col ).ToList();
3329+
3330+
string rel = GetRelationship(relationship, fkCols2, pkCols, pkPropName, fkPropName, manyToManyMapping, mapKey, foreignKey.CascadeOnDelete, foreignKey.IncludeReverseNavigation, foreignKey.IsNotEnforced);
33303331
string com = Settings.IncludeComments != CommentsStyle.None ? " // " + foreignKey.ConstraintName : string.Empty;
3331-
fkCol.col.ConfigFk.Add(string.Format("{0};{1}", rel, com));
3332+
firstFKCol.col.ConfigFk.Add(string.Format("{0};{1}", rel, com));
33323333
}
33333334

33343335
if(foreignKey.IncludeReverseNavigation)
@@ -3362,41 +3363,51 @@ SELECT SERVERPROPERTY('Edition') AS Edition,
33623363
}
33633364
}
33643365

3365-
private static string GetRelationship(Relationship relationship, Column fkCol, Column pkCol, string pkPropName, string fkPropName, string manyToManyMapping, string mapKey, bool cascadeOnDelete, bool includeReverseNavigation, bool isNotEnforced)
3366+
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)
33663367
{
3367-
return string.Format("Has{0}(a => a.{1}){2}{3}",
3368-
GetHasMethod(relationship, fkCol, pkCol, isNotEnforced),
3368+
string hasMethod = GetHasMethod(relationship, fkCosl, pkCols, isNotEnforced);
3369+
string withMethod = GetWithMethod(relationship, fkCols, fkPropName, manyToManyMapping, mapKey, includeReverseNavigation);
3370+
3371+
return string.Format("{0}(a => a.{1}){2}{3}",
3372+
hasMethod,
33693373
pkPropName,
3370-
GetWithMethod(relationship, fkCol, fkPropName, manyToManyMapping, mapKey, includeReverseNavigation),
3374+
withMethod,
33713375
cascadeOnDelete ? string.Empty: ".WillCascadeOnDelete(false)");
33723376
}
33733377

33743378
// HasOptional
33753379
// HasRequired
33763380
// HasMany
3377-
private static string GetHasMethod(Relationship relationship, Column fkCol, Column pkCol, bool isNotEnforced)
3381+
private static string GetHasMethod(Relationship relationship, IList<Column> fkCols, IList<Column> pkCols, bool isNotEnforced)
33783382
{
3379-
bool withMany = false;
3380-
switch (relationship)
3381-
{
3382-
case Relationship.ManyToOne:
3383-
case Relationship.ManyToMany:
3384-
withMany = true;
3385-
break;
3386-
}
3387-
3388-
if (withMany || pkCol.IsPrimaryKey || pkCol.IsUniqueConstraint || pkCol.IsUnique)
3389-
return fkCol.IsNullable || isNotEnforced ? "Optional" : "Required";
3383+
bool withMany = ( relationship == Relationship.ManyToOne || relationship == Relationship.ManyToMany );
3384+
bool fkIsNullable = fkCols.Any( c => c.IsNullable );
3385+
bool pkIsUnique = pkCols.Any( c => c.IsUnique || c.IsUniqueConstraint || c.IsPrimaryKey );
3386+
3387+
if ( withMany || pkIsUnique )
3388+
{
3389+
if( fkIsNullable || isNotEnforced )
3390+
{
3391+
return "HasOptional";
3392+
}
3393+
else
3394+
{
3395+
return "HasRequired";
3396+
}
3397+
}
3398+
else
3399+
{
3400+
return "HasMany";
3401+
}
33903402

3391-
return "Many";
33923403
}
33933404

33943405
// WithOptional
33953406
// WithRequired
33963407
// WithMany
33973408
// WithRequiredPrincipal
33983409
// WithRequiredDependent
3399-
private static string GetWithMethod(Relationship relationship, Column fkCol, string fkPropName, string manyToManyMapping, string mapKey, bool includeReverseNavigation)
3410+
private static string GetWithMethod(Relationship relationship, IList<Column> fkCols, string fkPropName, string manyToManyMapping, string mapKey, bool includeReverseNavigation)
34003411
{
34013412
string withParam = includeReverseNavigation ? string.Format("b => b.{0}", fkPropName) : string.Empty;
34023413
switch (relationship)
@@ -3408,7 +3419,7 @@ SELECT SERVERPROPERTY('Edition') AS Edition,
34083419
return string.Format(".WithRequiredDependent({0})", withParam);
34093420

34103421
case Relationship.ManyToOne:
3411-
if (!fkCol.Hidden)
3422+
if (!fkCols.Any( c => c.Hidden ))
34123423
return string.Format(".WithMany({0}).HasForeignKey({1})", withParam, manyToManyMapping); // Foreign Key Association
34133424
return string.Format(".WithMany({0}).Map(c => c.MapKey({1}))", withParam, mapKey); // Independent Association
34143425

0 commit comments

Comments
 (0)