【问题标题】:Entity Framework - Lazy Loading Self Referencing Collections实体框架 - 延迟加载自引用集合
【发布时间】:2013-11-24 23:17:15
【问题描述】:

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

期望:通过{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");
                    });
    }
} 

【问题讨论】:

    标签: c# .net database entity-framework


    【解决方案1】:

    feats.Include("PrerequisiteFeats").SingleOrDefault(x => x.Id == 2)

    这基本上会在同一个查询中同时查询专长和先决专长。它将 2 个单独的查询合二为一。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多