我如何在实体框架中手动创建关联 [英] How do i manually create a association in Entity Framework

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

问题描述

如果我使用VS2008创建现有数据库的实体框架模型,则如果在数据库中定义了基础表之间的关系,则会创建实体之间的关系.如果数据库中没有任何关系,显然不会发生这种情况.

假设数据库有2个表,例如:

文章:
-RecId:Int32//主键
-ArticleName
-....
订单行:
-RecId:Int32//主键
-ArticleId:Int32
-....

其中OrderLines表中的ArticleId字段用作对Articles表的引用.但是,数据库不包含此关系,并且由于技术原因,我无法/不允许更改数据库结构.

我的问题是:如何在实体框架设计器中创建实体之间的关联,以便可以使用类似以下的模型:
字符串artName = MyOrderLine.Article.ArticleName;

在设计器中,我尝试选择一个实体,右键单击,然后在弹出菜单中选择添加关联"菜单项.
在下面的对话框中,我选择了必须关联的表及其多样性,但是我无法在任何地方看到必须关联的字段.这就是OrderLines.ArticleId和Articles.RecId.

同样,我似乎无法使用映射详细信息"平面来编辑实体之间的关系.
在Google上搜索"Entity framework association"等,结果超过了800.000次点击似乎没有给出答案....

TBH,我不明白为什么很难在两个表之间建立关系-毕竟这应该是关系数据库/实体模型中要做的最基本的事情之一.

/NIP

If I create an entity framework model of an existing database using VS2008 then relations between entities are created if there in the database is defined relations between the underlying tables. This obviously doesn''t happen if there are no relations in the DB.

Assume that the database have 2 tables like:

Articles:
- RecId : Int32 // Primary Key
- ArticleName
-....
OrderLines:
- RecId : Int32 // Primary key
- ArticleId : Int32
- ....

Where the ArticleId field in the OrderLines table is used as a reference to the Articles table. The database doesn''t however include this relation and for technical reasons I am not able/allowed to change the database structure.

My question is: How do I in the Entity Framework Designer create the association between the entities, so that I can use the model like:
string artName = MyOrderLine.Article.ArticleName;

In the designer I have tried to select an entity, right click and in the pop-up menu selected the Add association menuitem.
In the following dialogbox I select the tables that has to be associated and its multiplicity, but nowhere can I see which fields has to be related. Here that would be OrderLines.ArticleId and Articles.RecId.

Likewise I doesn''t seem to be able to use the Mapping Details plane to edit the relation between the entities.
Searching for "Entity framework association" etc. on Google gives more than 800.000 hit that doesn''t seem to give the answer....

Tbh, I don''t get why it is so hard to make a relation between two tables - after all that should be one of the most basic things to do in a relationship database/entity model.

/NIP

推荐答案

写道:​​

在下面的对话框中,我选择了必须关联及其多样性,但我无处可看到必须关联的字段.这就是OrderLines.ArticleId和Articles.RecId.

In the following dialogbox I select the tables that has to be associated and its multiplicity, but nowhere can I see which fields has to be related. Here that would be OrderLines.ArticleId and Articles.RecId.



对此的设计者设计有点愚蠢/违反直觉的IMO.
您需要在关联中选择两者父表和子表,然后这些字段才会出现.选择参与关系的人,然后单击确定".

关联将显示为箭头,您可以右键单击它以访问其属性,例如基数和所涉及属性的名称等.



The designer design for this is a bit stupid / counter intuative IMO.
You need to select both parent and child tables in the association before the fields appear. Select the ones participating in the relationship, and click OK.

The association will appear as an arrow, you can right click this to access its properties, such as Cardinality and name of the properties involved etc.


这篇关于我如何在实体框架中手动创建关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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