2012-04-24 111 views
2

我想急於負載,像這樣一些子實體:貪婪加載實體框架導航屬性錯誤

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

回答

3

您需要使用ICollection代替IEnumerable,因爲EF要求您的導航屬性被定義爲ICollection<T>.

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

太好了!謝謝!這也可以解釋一些其他奇怪的行爲!在花費半天的時間之前,應該先來這裏過網。 – Matt 2012-04-24 08:17:26