C# 实体框架-延迟加载自引用集合

C# 实体框架-延迟加载自引用集合,c#,.net,database,entity-framework,C#,.net,Database,Entity Framework,问题:我可以保存到自引用集合,但实体框架在保存到数据库后不会在集合中显示它们 所需:通过{entity}.{collection}.{query()}访问集合中的实体 实体: class Feat { public Feat() { PrerequisiteFeats = new HashSet<Feat>(); } public int Id { get; set; } // Other properties here

问题:我可以保存到自引用集合,但实体框架在保存到数据库后不会在集合中显示它们

所需:通过
{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");
                    });
    }
} 
职业专长
{
功绩
{
PrerequisiteFeats=newHashSet();
}
公共int Id{get;set;}
//这里还有其他物业吗
公共虚拟ICollection先决条件feats{get;set;}
}
上下文:

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");
                    });
    }
} 
类PathfinderContext:DbContext
{
公共数据库集专长{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity()
.HasMany(feat=>feat.prerequisitefeat)
.有很多
.Map(m=>
{
m、 MapLeftKey(“FeatId”);
m、 MapRightKey(“先决条件FeatId”);
m、 ToTable(“先决条件”);
});
}
} 
专长。包括(“先决条件吃”).SingleOrDefault(x=>x.Id==2)

这将基本上在同一个查询中查询专长和先决专长。它将两个独立的查询合并为一个