C# 实体类型不声明导航类型

C# 实体类型不声明导航类型,c#,entity-framework,dbcontext,C#,Entity Framework,Dbcontext,我在谷歌上搜索了这个错误,并尝试了许多不同的解决方案,但没有任何效果。当我尝试使用渴望加载来获取特定实体的相关实体时,我收到以下错误: A specified Include path is not valid. The EntityType 'Link' does not declare a navigation property with the name 'Like'. 我在DBContext中的OnModelCreating方法中尝试了许多不同的解决方案,但始终收到相同的错误。我最终创

我在谷歌上搜索了这个错误,并尝试了许多不同的解决方案,但没有任何效果。当我尝试使用渴望加载来获取特定实体的相关实体时,我收到以下错误:

A specified Include path is not valid. The EntityType 'Link' does not declare a navigation property with the name 'Like'.
我在DBContext中的OnModelCreating方法中尝试了许多不同的解决方案,但始终收到相同的错误。我最终创建了另一个项目,并使用Entity Framework Power Tools为我自动生成所有内容(以防我只是手工做了一些不正确的事情),但仍然会出错

非常感谢您为我指明解决此问题的正确方向。我已经坚持了一段时间,它阻止了我前进

以下是我如何尝试使用即时加载:

using(var context = new LPContext())
{
    var links = context.ViewLinks.Include("Link.Like").ToList();
}
public partial class ViewLink
{
    public string OverrideLinkName { get; set; }

    public string OverrideDescription { get; set; }

    public bool IsActive { get; set; }

    public int ViewLinkID { get; set; }

    public Nullable<int> ViewID { get; set; }

    public Nullable<int> LinkID { get; set; }

    public Nullable<int> CreatedByID { get; set; }

    public virtual Link Link { get; set; }
}

public partial class Link
{
    public Link()
    {
        this.Likes = new List<Like>();
        this.Visits = new List<Visit>();
    }

    public string LinkName { get; set; }

    public string Description { get; set; }

    public string WebsiteURL { get; set; }

    public string ImageData { get; set; }

    public string CreatedBy { get; set; }

    public Nullable<System.DateTime> CreatedOn { get; set; }

    public Nullable<System.DateTime> LastModified { get; set; }

    public int LinkID { get; set; }

    public Nullable<int> CreatedByID { get; set; }

    public virtual ICollection<Like> Likes { get; set; }

    public virtual ICollection<ViewLink> ViewLinks { get; set; }
}

public partial class Like
{
    public string LikedBy { get; set; }

    public Nullable<System.DateTime> LikedOn { get; set; }

    public int LikeID { get; set; }

    public Nullable<int> LinkID { get; set; }

    public Nullable<int> LikedByID { get; set; }

    public virtual Link Link { get; set; }
}
public class LinkMap : EntityTypeConfiguration<Link>
{
    public LinkMap()
    {
        // Primary Key
        this.HasKey(t => t.LinkID);

        // Properties
        this.Property(t => t.LinkName)
            .HasMaxLength(150);

        this.Property(t => t.Description)
            .HasMaxLength(255);

        this.Property(t => t.WebsiteURL)
            .HasMaxLength(255);

        this.Property(t => t.CreatedBy)
            .IsRequired()
            .HasMaxLength(50);

        // Table & Column Mappings
        this.ToTable("tblExplorerLinks");
        this.Property(t => t.LinkName).HasColumnName("LinkName");
        this.Property(t => t.Description).HasColumnName("Description");
        this.Property(t => t.WebsiteURL).HasColumnName("WebsiteURL");
        this.Property(t => t.ImageData).HasColumnName("ImageData");
        this.Property(t => t.CreatedBy).HasColumnName("CreatedBy");
        this.Property(t => t.CreatedOn).HasColumnName("CreatedOn");
        this.Property(t => t.LastModified).HasColumnName("LastModified");
        this.Property(t => t.LinkID).HasColumnName("LinkID");
        this.Property(t => t.CreatedByID).HasColumnName("CreatedByID");
    }
}

public class LikeMap : EntityTypeConfiguration<Like>
{
    public LikeMap()
    {
        // Primary Key
        this.HasKey(t => t.LikeID);

        // Properties
        this.Property(t => t.LikedBy)
            .IsRequired()
            .HasMaxLength(50);

        // Table & Column Mappings
        this.ToTable("tblExplorerLikes");
        this.Property(t => t.LikedBy).HasColumnName("LikedBy");
        this.Property(t => t.LikedOn).HasColumnName("LikedOn");
        this.Property(t => t.LikeID).HasColumnName("LikeID");
        this.Property(t => t.LinkID).HasColumnName("LinkID");
        this.Property(t => t.LikedByID).HasColumnName("LikedByID");

        // Relationships
        this.HasOptional(t => t.Link)
            .WithMany(t => t.Likes)
            .HasForeignKey(d => d.LinkID);
    }
}
以下是我的实体:

using(var context = new LPContext())
{
    var links = context.ViewLinks.Include("Link.Like").ToList();
}
public partial class ViewLink
{
    public string OverrideLinkName { get; set; }

    public string OverrideDescription { get; set; }

    public bool IsActive { get; set; }

    public int ViewLinkID { get; set; }

    public Nullable<int> ViewID { get; set; }

    public Nullable<int> LinkID { get; set; }

    public Nullable<int> CreatedByID { get; set; }

    public virtual Link Link { get; set; }
}

public partial class Link
{
    public Link()
    {
        this.Likes = new List<Like>();
        this.Visits = new List<Visit>();
    }

    public string LinkName { get; set; }

    public string Description { get; set; }

    public string WebsiteURL { get; set; }

    public string ImageData { get; set; }

    public string CreatedBy { get; set; }

    public Nullable<System.DateTime> CreatedOn { get; set; }

    public Nullable<System.DateTime> LastModified { get; set; }

    public int LinkID { get; set; }

    public Nullable<int> CreatedByID { get; set; }

    public virtual ICollection<Like> Likes { get; set; }

    public virtual ICollection<ViewLink> ViewLinks { get; set; }
}

public partial class Like
{
    public string LikedBy { get; set; }

    public Nullable<System.DateTime> LikedOn { get; set; }

    public int LikeID { get; set; }

    public Nullable<int> LinkID { get; set; }

    public Nullable<int> LikedByID { get; set; }

    public virtual Link Link { get; set; }
}
public class LinkMap : EntityTypeConfiguration<Link>
{
    public LinkMap()
    {
        // Primary Key
        this.HasKey(t => t.LinkID);

        // Properties
        this.Property(t => t.LinkName)
            .HasMaxLength(150);

        this.Property(t => t.Description)
            .HasMaxLength(255);

        this.Property(t => t.WebsiteURL)
            .HasMaxLength(255);

        this.Property(t => t.CreatedBy)
            .IsRequired()
            .HasMaxLength(50);

        // Table & Column Mappings
        this.ToTable("tblExplorerLinks");
        this.Property(t => t.LinkName).HasColumnName("LinkName");
        this.Property(t => t.Description).HasColumnName("Description");
        this.Property(t => t.WebsiteURL).HasColumnName("WebsiteURL");
        this.Property(t => t.ImageData).HasColumnName("ImageData");
        this.Property(t => t.CreatedBy).HasColumnName("CreatedBy");
        this.Property(t => t.CreatedOn).HasColumnName("CreatedOn");
        this.Property(t => t.LastModified).HasColumnName("LastModified");
        this.Property(t => t.LinkID).HasColumnName("LinkID");
        this.Property(t => t.CreatedByID).HasColumnName("CreatedByID");
    }
}

public class LikeMap : EntityTypeConfiguration<Like>
{
    public LikeMap()
    {
        // Primary Key
        this.HasKey(t => t.LikeID);

        // Properties
        this.Property(t => t.LikedBy)
            .IsRequired()
            .HasMaxLength(50);

        // Table & Column Mappings
        this.ToTable("tblExplorerLikes");
        this.Property(t => t.LikedBy).HasColumnName("LikedBy");
        this.Property(t => t.LikedOn).HasColumnName("LikedOn");
        this.Property(t => t.LikeID).HasColumnName("LikeID");
        this.Property(t => t.LinkID).HasColumnName("LinkID");
        this.Property(t => t.LikedByID).HasColumnName("LikedByID");

        // Relationships
        this.HasOptional(t => t.Link)
            .WithMany(t => t.Likes)
            .HasForeignKey(d => d.LinkID);
    }
}
如果还有什么我可以提供的,我会这样做。提前感谢。

错误是正确的

Link
没有
Like
属性,它有
Likes
属性。一个
链接可以有很多
喜欢的
。如果要加载所有
链接
,只需修复包含字符串:

using(var context = new LPContext())
{
    var links = context.ViewLinks.Include("Link.Likes").ToList();
}

而且…我现在觉得自己非常愚蠢。谢谢你的帮助。哦,不用担心。很难发现。在EF 5.0及以上版本中,您可以避开硬编码字符串,使用lambda表达式(例如
context.ViewLinks.Include(vl=>vl.Link.Likes).ToList()
)。不幸的是,我必须使用硬编码字符串。我的公司有一个定制的框架,我必须使用它来传递加载字符串。上面的例子看起来不像我将在生产中使用的确切代码,但它是相同的概念,并且产生了相同的错误。这里没有人会对公司的独特架构有答案,你知道吗?再次感谢。哦,是的,我知道:)答案很简单,如果它解决了你的问题,别忘了接受它。OTOH,由于一个简单的印刷错误,我将投票结束。