指定的包含路径无效。实体框架代码优先

时间:2014-12-18 17:15:23

标签: entity-framework ef-code-first lazy-loading

尝试在EF中运行查询时出现以下错误。 指定的包含路径无效。 EntityType' InvestmentClub'不会声明名为' ClubMembers'。

的导航属性

查询

return _context.InvestmentClubs.Where(x => x.InvestmentClubId == clubId)
                       .Include(x => x.Policies).Include(x => x.ClubMembers).FirstOrDefault();

投资俱乐部课程

public class InvestmentClub
{
    public int InvestmentClubId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public int CreatedBy { get; set; }
    public DateTime DateCreated { get; set; }
    public InvestmentClubType ClubType { get; set; }

    //Navigation properties
    [ForeignKey("CreatedBy")]
    public ApplicationUser ApplicationUser { get; set; }
    public ICollection<InvestmentClubMember> ClubMembers;
    public ICollection<InvestmentClubPolicy> Policies;
}

InvestmentClubPolicy类

public class InvestmentClubPolicy
{
    public int InvestmentClubPolicyId { get; set; }
    public int InvestmentClubId { get; set; }
    public DateTime DateAdded { get; set; }
    public string PolicyStatement { get; set; }
    public AjoPolicyStatus PolicyStatus { get; set; }
    public InvestmentClub InvestmentClub { get; set; }
}

我没有将属性指定为虚拟属性,因为我不想急切加载相关对象。这可能是错误的原因吗?热切期待所有的帮助。

2 个答案:

答案 0 :(得分:2)

public ICollection<InvestmentClubMember> ClubMembers;

应该是

public virtual ICollection<InvestmentClubMember> ClubMembers { get; set; }

......或者更好,

public virtual ICollection<InvestmentClubMember> ClubMembers { get; protected set; }

它必须是virtual才能使动态代理子类能够填充导航属性。他们不会因为属性为virtual而自动加载。

.Include扩展方法就是热切加载。

答案 1 :(得分:0)

ClubMembers被声明为字段,但EntityFramework要求它是一个属性。声明如下:

public ICollection<InvestmentClubMember> ClubMembers { get; set; };