实体框架模型首先:如何创建与属性的关联 [英] Entity Framework Model First: how to create association with properties

查看:132
本文介绍了实体框架模型首先:如何创建与属性的关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为Node和Edge创建一个图形的数​​据模型。如果Edge不包含任何属性,我可以简单地从Node创建多对多关联。但是我想在Edge上存储一些属性,例如距离。我试图创建另一个实体,但没有找到一种方法来声明Edge和Node之间的关系。是否可能在模型第一?如何?

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?

推荐答案

您必须从Node到Edge创建两个一对多关联来建模多对多的自引用与映射连接表的关系。在图论方面,EF模型创建定向图,所以它从A到B和B到A之间的边界不同。

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:

这篇关于实体框架模型首先:如何创建与属性的关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆