创建一个主表,其中两个子表与 EF 4.1 进行一对一或一对一的链接

发布于 2025-01-01 07:39:55 字数 1695 浏览 1 评论 0原文

使用 MVC EF4.1,我尝试将表 (TableMaster) 链接到 TableChildOne(一对零或一的关系)以及 TableChildTwo(也是一对零或一的关系)。 TableChildOne 和 TableChildTwo 不直接链接。

TablechildOne 和 TableChildTwo 需要共享 TableMaster 的主键(我读到这是不可能的,有任何解决方法吗?)

我包含一个图像以使这一点更清楚,不确定是否应该在某处添加外键,这不是由代码创建的实际模型,但这正是我想要的。不确定某个地方是否应该有外键?

图片:http://www.davidsmit.za.net/img/untitled.png

我下面的代码可以编译,但是当尝试添加控制器时,我收到错误:

“已添加具有相同键的项目”

  public class TableMaster
{
    public int TableMasterID { get; set; }

    public DateTime ReportDate { get; set; }

    public virtual TableChildOne TableChildOne { get; set; }
    public virtual TableChildTwo TableChildTwo { get; set; }
}

public class TableChildOne
{
    [Key]
    public int TableMasterID { get; set; }

    public String Description_1 { get; set; }

    public virtual TableMaster TableMaster { get; set; }
}

  public class TableChildTwo
  {
     [Key]
      public int TableMasterID { get; set; }

     public String Description_2 { get; set; }

     public virtual TableMaster TableMaster { get; set; }
}

public class Context : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<TableMaster>()
            .HasOptional(p => p.TableChildOne).WithRequired(p => p.TableMaster);

        modelBuilder.Entity<TableMaster>()
            .HasOptional(p => p.TableChildTwo).WithRequired(p => p.TableMaster);

}

当我完全删除第二个表时,它工作正常。

我使用下面的链接作为示例(表 OfficeAssignment 和 Student),它展示了如何以一到零或一的方式链接表。但我无法添加具有相同链接的另一个表:

任何帮助将不胜感激。

谢谢 阿佩尔梅斯特

Using MVC EF4.1, I am trying to link a table (TableMaster) to TableChildOne (relationship one-to-zero-or-one) and also to TableChildTwo (also one-to-zero-or-one).
TableChildOne and TableChildTwo are not directly linked.

TablechildOne and TableChildTwo needs to share the primary key of TableMaster (I read this is not possible, any workarounds?)

I am including an image to make this a bit more clear, not sure if there should be foreign keys added somewhere, this is not an actual model created by the code, but is what i would like. not sure if there should be foreign keys somewhere?

image : http://www.davidsmit.za.net/img/untitled.png

My code below compiles, but when trying to add a controller, I get the error :

"An item with the same key has already been added"

  public class TableMaster
{
    public int TableMasterID { get; set; }

    public DateTime ReportDate { get; set; }

    public virtual TableChildOne TableChildOne { get; set; }
    public virtual TableChildTwo TableChildTwo { get; set; }
}

public class TableChildOne
{
    [Key]
    public int TableMasterID { get; set; }

    public String Description_1 { get; set; }

    public virtual TableMaster TableMaster { get; set; }
}

  public class TableChildTwo
  {
     [Key]
      public int TableMasterID { get; set; }

     public String Description_2 { get; set; }

     public virtual TableMaster TableMaster { get; set; }
}

public class Context : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<TableMaster>()
            .HasOptional(p => p.TableChildOne).WithRequired(p => p.TableMaster);

        modelBuilder.Entity<TableMaster>()
            .HasOptional(p => p.TableChildTwo).WithRequired(p => p.TableMaster);

}

When I remove the second table completely, it works fine.

I used the below link as an example (tables OfficeAssignment and Student), which shows how to link a table one-to-zero-or-one. But I have trouble adding another table with the same linkage:
http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/creating-a-more-complex-data-model-for-an-asp-net-mvc-application

Any help will be appreciated.

Thanks
appelmeester

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

記柔刀 2025-01-08 07:39:55

您能否提供更多背景信息来说明为什么要这样做?如果您在三个表之间共享主键,则您正在对数据进行分区。您想要解决什么开发场景。听起来您可能想要映射对象继承,是吗?

如果您确实只有几个描述,那么这实际上只是一张表。

编辑:

酷。因为这个请求的业务上下文有点模糊,我还是不太明白,抱歉。如果您有一个 TableMaster 和一些子表,那么这听起来像一个继承树。因此,使用 EF,您可以选择许多不同的策略来对此进行建模(TPH、TPT 等)。为此,我建议研究 TPT,因为这可能会让您了解如何清理数据的粒度。此外,您还可以获得默认情况下创建表的好处,很大程度上就像您指定的那样。检查 这个 供参考。

Could you give more background about why you want to do this? If you are sharing the primary key across three tables you are partitioning data. What development scenario are you trying to address. It sounds like you might be wanting to map an object inheritance, is that right?

If you truly only have a couple of Descriptions, then this is really just one table.

EDIT:

Cool. Because the business context of this request is a bit vague, I can't quite understand still, sorry. If you have a TableMaster and then some child tables, then this sounds like an inheritance tree. So with EF, you can choose many different strategies to model this (TPH, TPT etc). For this, I would suggest looking into TPT because this might allow you to get the granularity for how you want to clean up the data. Also, you get the benefit that the tables will be created, by default, largely like you have specified. Check this out for reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文