【问题标题】:Eager Load Entity Framework Navigation Property error急切加载实体框架导航属性错误
【发布时间】:2012-04-24 04:38:31
【问题描述】:

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

_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 中

我错过了什么???谢谢!

POCO:

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; }
    }
}

【问题讨论】:

    标签: entity-framework navigation poco eager-loading


    【解决方案1】:

    您需要使用ICollection 而不是IEnumerable,因为EF 要求您的导航属性定义为ICollection&lt;T&gt;.

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

    【讨论】:

    • 太棒了!谢谢!这也可以解释其他一些奇怪的行为!应该先来这里再花半天时间拖网。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-09
    • 1970-01-01
    • 1970-01-01
    • 2011-04-09
    相关资源
    最近更新 更多