I have the following table structure:
Affiliations
Id Party1 Party2
1 100 200
2 300 400
And a table referenced through the FKs of Affiliations
Parties
Id Name
100 Hank
200 Ted
300 Kim
400 Joe
Using my Party entity (not shown), I define a relationship between the tables using Fluent API:
modelBuilder.Entity<Party>()
.HasMany(c => c.LeftPartyAffiliations)
.WithRequired()
.Map(m => m.MapKey("Party1"));
modelBuilder.Entity<Party>()
.HasMany(c => c.RightPartyAffiliations)
.WithRequired()
.Map(m => m.MapKey("Party2"));
The party entity maps left and right relationships to their respective properties, LeftPartyAffiliations and RightPartyAffiliations. By defining the rule above, EF automatically adds Party1 and Party2 to my Affiliations table.
My problem lies in the fact that while the values in each affiliation map correctly, I don't have a way of knowing what the affiliation Id on the left and right sides, as I am mapping these tables to my columns through my Party relationship.
For instance, I would like to be able to access the actual Party1 and Party2 Id's via my Affiliation entity:
public class PartyAffiliation
{
...
[Column("Party1")]
public int Party1 { get; set; }
[Column("Party2")]
public int Party2 { get; set; }
...
}
...but this is impossible as I am already defining my relationship through mapping to my Left and Right collections, from Party, via Fluent API.
A perfect situtation would allow me to do the Fluent API mappings, which would create the columns and relationships as well as allowing me to access my Party1 and Party2 Id's via the physical columns defined in my Affiliation mapping.
If I attempt to define the properties phyically in the entity as well as in Fluent API, I get:
Each property name in a type must be unique. Property name was already defined 'xxx'
Is there a workaround to this issue or a way of mapping a column to multiple properties without physically adding an additional field to the database?