0

Document、、、の3つのモデルがSectionありParagraphます。それぞれがこのように見えます。

// Document
public class Document
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Section> Sections { get; set; }
}

// Section
public class Section
{
    public int Id { get; set; }
    public int DocumentId { get; set; }
    public virtual Document Document { get; set; }
    public virtual ICollection<Paragraph> Paragraphs { get; set; }
}

// Paragraph
public class Paragraph
{
    public int Id { get; set; }
    public int SectionId { get; set; }
    public virtual Section Section { get; set; }
}

Section.Paragraphsエンティティは、。のすべての段落を自動的に入力しSectionId == Idます。ただし、これは発生していませんDocument.Sections。、がnullDocument.Sectionsであるすべてのセクションが入力される代わりに、ああ!DocumentId == idDocument.Sections

4

1 に答える 1

0

次の注釈を追加します。

// Document
public class Document
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }

    [InverseProperty("Document")]
    public virtual ICollection<Section> Sections { get; set; }
}

// Section
public class Section
{
    [Key]
    public int Id { get; set; }
    [ForeignKey("Document")]
    public int DocumentId { get; set; }

    public virtual Document Document { get; set; }

    [InverseProperty("Section")]
    public virtual ICollection<Paragraph> Paragraphs { get; set; }
}

// Paragraph
public class Paragraph
{
    [Key]
    public int Id { get; set; }

    [ForeignKey("Section")]
    public int SectionId { get; set; }


    public virtual Section Section { get; set; }
}

私はこれも仮定するつもりです:

public class YourContext : DbContext
{
    public DbSet<Document> Documents {get;set;}
    public DbSet<Paragraph> Paragraphs {get;set;}
    public DbSet<Section> Sections {get;set;}
}

それが何らかの形であなたを助けたかどうか教えてください。エンティティのロード方法に問題がある可能性があります (インクルードを使用していますか)。

于 2012-05-05T11:42:30.123 に答える