Eager Load Entity Framework导航属性错误

时间:2012-04-24 04:38:32

标签: entity-framework navigation poco eager-loading

我正试图像这样急切加载一些子实体:

_context.Sites.Where(x => x.ID == siteID).Include(s => s.SiteLoggers).FirstOrDefault();

然而,我得到的错误是:

A specified Include path is not valid. The EntityType 'MyProject.Dal.EF.Site' does not declare a navigation property with the name 'SiteLoggers'.

说的是正确的,因为MyProject.Dal.EF.Site不存在,该对象存在于MyProject.Domain.Entities.Site

我缺少什么?谢谢!

波苏斯:

namespace MyProject.Domain.Entities
{
    public class Site
    {
        public int ID { get; set; }
        public int LocationID { get; set; }
        public bool Active { get; set; }
        public bool Deleted { get; set; }
        public string Name { get; set; }
        public virtual Location Location { get; set; }
        public virtual IEnumerable<SiteLogger> SiteLoggers { get; set; }
    }
}

namespace MyProject.Domain.Entities
{
    public class SiteLogger
    {
        public int ID { get; set; }
        public int UID { get; set; }
        public int SiteID { get; set; }
        public string Name { get; set; }
        public int LocationID { get; set; }
        public bool Active { get; set; }
        public bool Deleted { get; set; }
        public virtual Site Site { get; set; }
        public virtual Location Location { get; set; }
    }
}

1 个答案:

答案 0 :(得分:3)

您需要使用ICollection代替IEnumerable,因为EF要求您的导航属性定义为ICollection<T>.

 public virtual ICollection <SiteLogger> SiteLoggers { get; set; }