许多一对多的实体框架的关系有关系informantion [英] Many-to-Many relationship in Entity Framework with relationship informantion

查看:163
本文介绍了许多一对多的实体框架的关系有关系informantion的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先说,这是使用实体框架,所以我很抱歉,如果这最终是一个新手的问​​题我第一次经历。一切都进展顺利,到目前为止,但是,我碰到这种情况,我不知道该怎么处理最好的办法是。

First said, this is my first experience using the Entity Framework so I apologize if this ends up being a newbie question. Everything has gone well so far; but, I run into a situation that I am not sure what the best way to handle is.

我需要建立一个多一对多的关系;但是,在众多的对多表包含多于只是组合键。在这种情况下,实体框架没有看到将其识别为一个多到多结构,所以我似乎并没有得到轻松的能力得到了收藏不使用中间表。有没有更好的方式来做到这一点?

I need to create a many-to-many relationship; but, the many-to-many table contains more than just the composite key. In this case the Entity Framework does not see to recognize it as a many-to-many structure and so I don't seem to get the easy ability to get the collections without using the intervening table. Is there a better way to do this?

简单的例子:

  • 在一个单位可以有很多电路板和电路板可能已经在许多单位
  • 当它是在一个单元我们需要记录它是在什么时隙。

表:

Unit
 UnitID(PK)
 UnitName

Board
 BoardID(PK)
 BoardName

UnitBoard
 UnitID(PK,FK1)
 BoardID(PK,FK2)
 Slot

当我完成这在我的使用ADO.NET实体数据模型code,我没有看到一个简单的方法来从单位实体,反之亦然关联到单元板的集合。

When I pull this into my code using an ADO.NET Entity Data Model, I don't see an easy way to get the collection of Boards associated to a Unit from the Unit Entity or visa-versa.

有没有更好的方式来做到这一点还是我只需要使用UnitBoards的关联集合,然后用它来建造单位的集合/板?

Is there a better way to do this or do I just need to use the associated collection of UnitBoards then use that to build the collection of Units/Boards?

这似乎是我大概不会去这样做的第一人。例:我在想的书籍和业主希望还保留相关BookOwner信息(如购买之日起)的

This seems I am probably not the first person to went to do something like this. Ex: I was thinking of Books and Owners wanting to also keep the related BookOwner information (like the purchase date).

推荐答案

即使在更新的实体框架5,有针对此问题没有很好的解决方案。如果您有适当的外键关系,您应该能够访问您的主板或单位,如:

Even in the updated Entity Framework 5, there is no good solution for this problem. If you have the proper Foreign Key relationships you should be able to access your Boards or Units like:

Unit.UnitBoards.Boards

Board.UnitBoards.Units

但你不会有直接的导航性能(多对许多导航),如果多到多个表都有它的组合键之外自己的属性。

but you won't have a direct navigation properties (many-to-many navigations) if the many-to-many table has it's own properties outside of the composite key.

当过滤您可以用它们包括包含选择来减少数据库的调用。

When filtering you can include them using Include and Select to reduce db calls.

var myBoards = From Context.Boards
  .Include(i => i.UnitBoards)
  .Include(i => i.UnitBoards.Select(is => is.Unit))
  .Where(...)

这篇关于许多一对多的实体框架的关系有关系informantion的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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