簡體   English   中英

實體框架-延遲加載自引用集合

[英]Entity Framework - Lazy Loading Self Referencing Collections

問題:我可以保存到自引用集合,但是在保存到數據庫后,Entity Framework不會在集合中顯示它們。

查詢ID為22的Feat

EF生成的多對多表中ID為22的壯舉

Feat ID 22,與之無關

期望:{entity}.{collection}.{query()};訪問集合中的{entity}.{collection}.{query()};

實體:

class Feat
{
    public Feat()
    {
        PrerequisiteFeats = new HashSet<Feat>();
    }

    public int Id { get; set; }
    // Other properties here
    public virtual ICollection<Feat> PrerequisiteFeats { get; set; }
}

語境:

class PathfinderContext : DbContext
{
    public DbSet<Feat> Feats { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Feat>()
                    .HasMany(feat => feat.PrerequisiteFeats)
                    .WithMany()
                    .Map(m =>
                    {
                        m.MapLeftKey("FeatId");
                        m.MapRightKey("PrerequisiteFeatId");
                        m.ToTable("PrerequisiteFeats");
                    });
    }
} 

feats.Include(“ PrerequisiteFeats”)。SingleOrDefault(x => x.Id == 2)

這基本上將在同一查詢中同時查詢功能和前提功能。 它將2個獨立的查詢合並為一個。

暫無
暫無

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

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