【问题标题】:How to map identifying relationship in Entity Framework 5 code first child entity with multiple mutually exclusive parent entities如何在 Entity Framework 5 代码第一个子实体中映射识别关系与多个互斥父实体
【发布时间】:2013-02-08 00:13:27
【问题描述】:

我使用的是 Entity Framework 5 Code First,我有以下模型:

class Document
{
    public int Id {get;set;}
    public String Name {get;set;}

    public IList<Page> Pages {get;set;}
}

class DocumentTemplate
{
    public int Id {get;set;}
    public String Description {get;set;}
    public String Name {get;set;}

    public IList<Page> Pages {get;set;}
}

class Page
{
    public int Id {get;set;}
    public string Text {get;set;}
}

我知道如何在子实体有 1 个父实体的情况下映射识别关系。但我想映射 Page 实体,以便它对每个父母都有一个识别关系。

此外,父关系是互斥的。特定页面将属于 DocumentTemplate 或 Document,而不是两者。

在 Entity Framework 5 中是否可以进行这样的映射?

我不想为页面创建单独的实体,因为它们本质上是相同的,除了父关系。

TIA。

【问题讨论】:

    标签: c# entity-framework-5 relationship


    【解决方案1】:

    我认为您不能有多个父母,但我会考虑以下选项:
    (任何文档都属于some-template,只有模板才有页面)

    class Document
    {
        public int Id {get;set;}
        public String Name {get;set;}
        public DocumentTemplate DocumentTemplate{get;set;}
    }
    
    class DocumentTemplate
    {
        public int Id {get;set;}
        public String Description {get;set;}
        public String Name {get;set;}
    
        public IList<Page> Pages {get;set;}
    }
    
    class Page
    {
        public int Id {get;set;}
        public string Text {get;set;}
    }
    

    【讨论】:

      【解决方案2】:

      这对你有用:

      class Page
      {
          public int Id {get;set;}
          public string Text {get;set;}
      
          public int? DocumentId { get; set; } // non-mandatory relationship to Document
          public int? DocumentTemplateId { get; set; } // non-mandatory relationship to DocumentTemplate
      
          // ... navigation properties
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多