I'm in a situation where I have a table called Elements
. Now I'm creating a table called Divergences
that will store basically pairs of Elements
. The purpose of Divergence
is to check if two Elements
have diverging answers.
Element Divergence
--------- ----------
ElementId ElementId1
ElementId2
In the above table schema, ElementId1
and ElementId2
are Foreign Keys mapping to ElementId
in Elements
table and form the composite primary key for the Divergence
table.
I use Database First approach where I create the tables in SQL Server Management Studio and later I do a Update Model from Database...
in Entity Framework Designer.
The problem I'm facing is that when EF Designer generates the model it creates 2 sets ICollection<Element>
inside the Element
class namely Elements
and Elements1
.
This doesn't give me a Divergences
DbSet.
What I'd want to have is a Divergence
class where I'd do something like this in code:
Divergence d = new Divergence();
d.Element1 = element1;
d.Element2 = element2;
Database.Divergences.Add(d);
Database.SaveChanges();
and later on:
Element e = Database.Elements.Single(e => e.ElementId == 7);
var divergences = e.Divergences;
I tried adding a new column to the Divergence
table like this:
Element Divergence
--------- ------------
ElementId DivergenceId
ElementId1
ElementId2
This correctly leads to a 1 <-> *
relationship in Entity Framework Designer. I finally get a Divergences
DbSet but DivergenceId
property is useless in the code and I still get the 2 sets in the Element
class. It's important to note that ElementId1
and ElementId2
still form the composite primary key.
What do you think is the correct way of mapping this specific situation? Thanks for the input.