Entity Framework Model First: how to create association with properties

933 views Asked by At

I am trying to create data model for a graph with Node and Edge. If Edge does not contain any property I can simply create many-to-many association from a Node to itself. However I want to store some properties on the Edge e.g. Distance. I tried to create another entity but didn't find a way to declare the relationship between Edge and Node. Is it possible in Model First? How?

1

There are 1 answers

0
Ladislav Mrnka On BEST ANSWER

You must create two one-to-many associations from Node to Edge to model self referencing many-to-many relation with mapped junction table. In terms of graph theory EF models creates oriented graph so it differs between edge from A to B and from B to A.

You will start with your two entities and their properties:

enter image description here

You will drag the first Association from Toolbox. Start at Node and drag association to Edge - it will create one-to-many relation between Node and Edge. Configure properties of created navigation for "Outgoing" edges:

enter image description here

You will drag the second Association in the same way and configure its properties for "Incoming" edges:

enter image description here

After generating a database from this model you will get this table structure:

enter image description here