How to map an identity link in Entity Framework 5 to the first child code object with multiple mutually exclusive parent objects

I am using Entity Framework 5 Code First and I have the following model:

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

I know how to match an identifying relationship in which a child has 1 parent. But I would like to map the Page object so that it has an identifying relationship for each parent.

In addition, parental relationships are mutually exclusive. A separate page will either belong to a DocumentTemplate, or to a document, and not both.

Is such a mapping possible in Entity Framework 5?

I do not want to create separate objects for the page, because they will be essentially the same, except for the parent relationship.

TIA.

+5
2

, , :
( , )

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

:

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

All Articles