将关系映射到特定表
我有一个类,如下所示:
public class Node
{
public int NodeID { get; set; }
public virtual ICollection<Node> Froms { get; set; }
public virtual ICollection<Node> Tos { get; set; }
}
当代码首先将其映射到 2 个表时:
- 节点
- NodesNodes
以及它们之间的两个 一对多 关系,
现在的问题是我如何使 ef 制作如下 3 个表:
- 节点
- Froms
- Tos
和两个一对多节点和Froms之间的关系加上节点和Tos
我在我的asp.net帖子上放了两个图像可能会有所帮助 asp.net 帖子
更多描述
让我详细描述一下现有的问题:
我正在对一个管道系统进行建模,该系统的结构类似于我已经提到的节点。一条管道可以从许多其他管道开始,也可以以许多其他管道结束,例如:1 号管道从 2,3,4 号开始,到 5 和 6 号结束。
实体框架将管道(节点)类映射到两个表,如下所示:
Nodes ========= ID --------- 1 2 3 4 5 6
NodeNodes ============================ Node_NodeID Node_NodeID1 ---------------------------- 1 2 1 3 1 4 5 1 6 1
太棒了! EF 非常聪明地将 Starts 和 Ends 放在一张表中,但主要问题是什么?!
当用户定义 1 号管道的开始和结束时,如果他查看 2,3 和 4 将看到它们以 1 结尾,如果他查看 5 和 6,他将看到它们从 1 开始。这些附加信息(当然它们是真的)是不希望的。
我思考解决方案并提出一个想法,如果我有两个 FROM 和 TO 表格,我就不会解释问题。
例如:
Froms ============================ Node_NodeID From_NodeID1 ---------------------------- 1 2 1 3 1 4
Tos ============================ Node_NodeID To_NodeID1 ---------------------------- 1 5 1 6
I have a class as below:
public class Node
{
public int NodeID { get; set; }
public virtual ICollection<Node> Froms { get; set; }
public virtual ICollection<Node> Tos { get; set; }
}
When the Code First mapped it to 2 tables:
- Nodes
- NodesNodes
And two One to Many Relationship between them,
Now the question is how I can made ef to make 3 Tables as below:
- Nodes
- Froms
- Tos
And two One to Many Relationship between Nodes and Froms plus Nodes and Tos
I put two images on my asp.net post that may help
asp.net post
MORE DESCRIPTION
Let me describe more, the existing problem:
I am modeling a pipeline system that has a structure like the NODE I have already mentioned. A pipeline may start from many other pipelines and may ends to many another pipelines, for example: number 1 pipeline starts from number 2,3,4 and ends to number 5 and 6 .
The entity framework mapped the pipeline (Node) class to TWO tables as below:
Nodes ========= ID --------- 1 2 3 4 5 6
NodeNodes ============================ Node_NodeID Node_NodeID1 ---------------------------- 1 2 1 3 1 4 5 1 6 1
Great! The EF very smartly put the Starts and Ends in one table, but what the main problem is?!
When a user define starts and ends of number 1 pipeline , if he look at 2,3 and 4 will see they ends to 1 and if he go to 5 and 6 he will see they begins from 1. This additional information (of course they are true) is not desired.
I think about the solution and come up with an idea, if I have two tables for FROMs and TOs ,I won’t have explained problem.
For example:
Froms ============================ Node_NodeID From_NodeID1 ---------------------------- 1 2 1 3 1 4
Tos ============================ Node_NodeID To_NodeID1 ---------------------------- 1 5 1 6
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种情况下,您的关系不能是
Node
类,但您必须创建新的From
和To
类。编辑:
EF 无法以这种方式将多个表映射到同一实体,而且它也没有任何意义,因为单个节点在某些情况下可以来自,而在其他情况下可以来自。
编辑2:
好的。这应该是可能的,但您必须单独维护每个关系。您必须使用流畅的 API 来映射它。在您的上下文中重写
OnModelCreating
方法:In such case your relations cannot be to
Node
class but you must make newFrom
andTo
class.Edit:
EF is not able to map multiple tables to the same entity this way and it also doesn't make any sense because single node can be from in some cases and to in other cases.
Edit2:
Ok. This should be possible but you will have to maintain each relation separately. You must use fluent API to map this. In your context override
OnModelCreating
method: