问题
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:
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:
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:
You will drag the second Association in the same way and configure its properties for "Incoming" edges:
After generating a database from this model you will get this table structure:
来源:https://stackoverflow.com/questions/9043453/entity-framework-model-first-how-to-create-association-with-properties