[英]Entity Framework - Lazy Loading Self Referencing Collections
問題:我可以保存到自引用集合,但是在保存到數據庫后,Entity Framework不會在集合中顯示它們。
期望:按{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.