实体类型不声明导航类型

本文关键字:类型 导航 声明 实体 | 更新日期: 2023-09-27 18:17:48

我在谷歌上搜索了这个错误,并尝试了许多不同的解决方案,但都不起作用。当我尝试使用Eager Loading获取特定实体的相关实体时,我收到以下错误:

A specified Include path is not valid. The EntityType 'Link' does not declare a navigation property with the name 'Like'.

我在DBContext中的OnModelCreating方法中尝试了许多不同的解决方案,但始终收到相同的错误。我最终创建了另一个项目,并使用实体框架电源工具为我自动生成一切(以防我只是手工做了一些错误的事情),但仍然得到错误。

任何帮助指出我在正确的方向来解决这个问题是非常感激的。我已经被它困了一段时间了,它阻碍了我的前进。

这是我如何尝试使用急切加载:

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

最后这里是OnModelCreating()方法

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new LikeMap());
        modelBuilder.Configurations.Add(new LinkMap());
        modelBuilder.Configurations.Add(new ViewLinkMap());
    }
如果还有什么我能提供的,我会做的。

实体类型不声明导航类型

错误是正确的。

Link没有Like的性质,它有Likes的性质。一个Link可以有多个Likes。如果你想加载所有LinkLikes,只需修改你的include字符串:

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