将关系映射到特定表

发布于 2025-01-02 23:14:38 字数 1789 浏览 4 评论 0原文

我有一个类,如下所示:

    public class Node
    {
         public int NodeID { get; set; }

         public virtual ICollection<Node> Froms { get; set; }
         public virtual ICollection<Node> Tos { get; set; }
    }

当代码首先将其映射到 2 个表时:

  1. 节点
  2. NodesNodes

以及它们之间的两个 一对多 关系,

现在的问题是我如何使 ef 制作如下 3 个表:

  1. 节点
  2. Froms
  3. 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:

  1. Nodes
  2. NodesNodes

And two One to Many Relationship between them,

Now the question is how I can made ef to make 3 Tables as below:

  1. Nodes
  2. Froms
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

牵你手 2025-01-09 23:14:38

在这种情况下,您的关系不能是 Node 类,但您必须创建新的 FromTo 类。

public class To { ... }
public class From { ... }

public class Node
{
     public int NodeID { get; set; }

     public virtual ICollection<From> Froms { get; set; }
     public virtual ICollection<To> Tos { get; set; }
}

编辑:

EF 无法以这种方式将多个表映射到同一实体,而且它也没有任何意义,因为单个节点在某些情况下可以来自,而在其他情况下可以来自。

编辑2:

好的。这应该是可能的,但您必须单独维护每个关系。您必须使用流畅的 API 来映射它。在您的上下文中重写 OnModelCreating 方法:

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{
    modelBuilder.Entity<Node>()
                .HasMany(n => n.Froms)
                .WithMany()
                .Map(m => m.ToTable("Froms"));

    modelBuilder.Entity<Node>()
                .HasMany(n => n.Tos)
                .WithMany()
                .Map(m => m.ToTable("Tos"));                    
}

In such case your relations cannot be to Node class but you must make new From and To class.

public class To { ... }
public class From { ... }

public class Node
{
     public int NodeID { get; set; }

     public virtual ICollection<From> Froms { get; set; }
     public virtual ICollection<To> Tos { get; set; }
}

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:

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{
    modelBuilder.Entity<Node>()
                .HasMany(n => n.Froms)
                .WithMany()
                .Map(m => m.ToTable("Froms"));

    modelBuilder.Entity<Node>()
                .HasMany(n => n.Tos)
                .WithMany()
                .Map(m => m.ToTable("Tos"));                    
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文