Skip to content

Mapping tables with composite keys#487

Open
csgalan wants to merge 2 commits intosjh37:masterfrom
csgalan:master
Open

Mapping tables with composite keys#487
csgalan wants to merge 2 commits intosjh37:masterfrom
csgalan:master

Conversation

@csgalan
Copy link
Copy Markdown

@csgalan csgalan commented Jan 31, 2019

This improve the handling of identification of mapping tables when include composite keys

@sjh37 sjh37 self-assigned this Feb 11, 2019
@sjh37
Copy link
Copy Markdown
Owner

sjh37 commented Feb 11, 2019

Thanks @cgalan12
This is causing a few issues with one of the large and nasty test databases I have. Tables with many fields used as primary keys seem to be missing. I will investigate further...

@sjh37
Copy link
Copy Markdown
Owner

sjh37 commented Feb 25, 2019

I understand what you are trying to do, but will have to leave this here for now as it's not working for all databases. I will return to this once EF 7 (Core) is complete.

@sjh37
Copy link
Copy Markdown
Owner

sjh37 commented Mar 27, 2026

This PR does the following:

Fixes mapping table detection for composite keys (EF6)

The old code assumed a mapping/junction table always has exactly 2 columns and 2 FK columns. This broke when the joined tables have composite primary keys (e.g. a many-to-many between two tables each with a 2-column PK would create a 4-column mapping table).

Changes:

  1. IdentifyMappingTable — removes the hardcoded nonReadOnlyColumns.Count != 2 check, replacing it with "all non-read-only columns must be primary key columns" (works for any number of composite key columns).
  2. FK distinctness check — switches from FkColumn.Distinct().Count() != 2 to ConstraintName.Distinct().Count() != 2, correctly identifying two distinct FK constraints regardless of column count.
  3. right FK selection — changed from foreignKeys[1] (index-based, fragile) to foreignKeys.Find(f => f.ConstraintName != left.ConstraintName) (correct for multi-column FKs).
  4. AddMappingConfiguration — new GetMapKeyName helper collects all columns for a given constraint and joins them as "col1", "col2" for MapLeftKey/MapRightKey.

It's not merge-ready as-is — it edits the old EF.Reverse.POCO.Core.ttinclude (v2 file), not the current Generator/ source files. The logic fix itself looks reasonable and addresses a real gap.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants