gpt4 book ai didi

entity-framework - 急切加载 Entity Framework 导航属性错误

转载 作者:行者123 更新时间:2023-12-04 05:49:09 24 4
gpt4 key购买 nike

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

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

最佳答案

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

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

关于entity-framework - 急切加载 Entity Framework 导航属性错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10291810/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com