簡體   English   中英

如何映射Entity Framework 5中的標識關系第一個子實體與多個互斥的父實體

[英]How to map identifying relationship in Entity Framework 5 code first child entity with multiple mutually exclusive parent entities

我正在使用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,而不是兩者。

實體框架5中是否可以進行這種映射?

我不想為Page創建單獨的實體,因為它們基本上是相同的,除了父關系。

TIA。

我不認為你可以有多個父母,但我會考慮以下選項:
(任何文檔屬於某個模板,只有模板可以有頁面)

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;}
}

這對你有用:

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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM