实体框架Include路径表达式必须引用在类型上定义的导航属性

时间:2014-09-11 16:47:15

标签: c# entity-framework lazy-loading

我有这个课程:

public class Activity
{
    public int Key { set; get; }
    public virtual ICollection<Tag> Tags { set; get; }
}

public class Hobby : Activity
    {
        public int SectionId { set; get; }
        public Section Section { set; get; }
    }

public class Location : Activity
    {
        public int StudioId { set; get; }
        public virtual Studio Studio{get;set;}
    }

public class Tag
    {
        public int Key { set; get; }
        public string Name { set; get; }
        public bool Valid { set; get; }

        public Tag()
        {
            this.Activities = new List<Activity>();
        }

        public virtual ICollection<Activity> Activities { set; get; }
    }

在Tag和Activity之间存在多对多的关系:

modelBuilder.Entity<Activity>().
              HasMany(c => c.Tags).
              WithMany(p => p.Activities).
              Map(
               m =>
               {
                   m.MapLeftKey("ActivityId");
                   m.MapRightKey("TagId");
                   m.ToTable("Pointer");
               });

现在在我的控制器中我试图这样做:为每个标签获取他指出的所有活动以及每个活动:工作室(如果活动是一个位置)/部分(如果活动是一种爱好)。要做到这一点,我必须告诉EF它必须延迟加载与标签相关的一些对象。我试着做这样的事情:

public IEnumerable<Tag> GetTags()
        {
            using (WelcomolidayDbContext db = new WelcomolidayDbContext())
            {
                return db.Tags
                    .Include("Activities")
                    .Include(t => t.Activities.Select(a => a.Creator))
                    .Include(t => t.Activities.Select(a => a.Pictures))
                    .Include(t => t.Activities.OfType<Hobby>().Select(a=>a.Section))
                    .Include(t => t.Activities.Select(a => a.Pictures))
                    .Include(t => t.Activities.OfType<Location>().Select(a => a.Studio))
                    .Include(t => t.Activities.Select(a => a.Tags)).ToList();
            }
        }

但我得到错误:

   The Include path expression must refer to a navigation property defined on the type. 
Use dotted paths for reference navigation properties and the Select operator for 
collection navigation properties.Parameter name: path

有人知道我做错了什么,或者是否有办法解决这个问题? 感谢。

亚历。

0 个答案:

没有答案