0

我有三个模型:DocumentSectionParagraph。每一个看起来都是这样的。

// 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. 而不是Document.Sections填充DocumentId == id,Document.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 回答