【问题标题】:The navigation on entity type has not been added to the model, or ignored, or entityType ignored实体类型的导航尚未添加到模型中,或被忽略,或 entityType 被忽略
【发布时间】:2016-04-04 07:10:21
【问题描述】:

实体类型“Notepad.Models.Note”上的导航“标签”尚未添加到模型中,或被忽略,或 entityType 被忽略。

public class Note
    {
        public Note()
        {
            CreationDate = DateTime.Now;
            Tags = new HashSet<Tag>();
            Parts = new HashSet<Part>();
        }

        public int ID { get; set; }
        public virtual ICollection<Tag> Tags { get; set; }
        public virtual ICollection<Part> Parts { get; set; }
        public DateTime? CreationDate { get; set; }
    }


public class Tag
    {
        public Tag()
        {
            Notes = new HashSet<Note>();
        }

        public int ID { get; set; }
        public string Name { get; set; }

        public virtual ICollection<Note> Notes { get; set; }
    }

添加迁移时会发生这种情况:

dnx ef 迁移添加 DbData -c DataDbContext

你认为为什么会这样?

编辑: 数据数据库上下文:

public class DataDbContext : DbContext
    {
        public DbSet<Note> Notes { get; set; }
        public DbSet<Tag> Tags { get; set; }
        public DbSet<Part> Parts { get; set; }
    }

【问题讨论】:

  • 显示Tag的代码
  • 您是否将 DbSet 添加到 DataDbContext 中?
  • @MarcinZablocki 我已经把它粘贴到问题上,你可以看看是否一切正确

标签: c# entity-framework asp.net-core dnx entity-framework-core


【解决方案1】:

你在那里有多对多关系。正如文档所说:http://docs.efproject.net/en/latest/modeling/relationships.html#id21

尚不支持没有实体类来表示连接表的多对多关系。但是,您可以通过包含连接表的实体类并映射两个单独的一对多关系来表示多对多关系。

所以你必须像这样创建额外的“加入”类:

public class NoteTag
    {
        public int NoteId { get; set; }
        public Note Note { get; set; }

        public int TagId { get; set; }
        public Tag Tag { get; set; }
    }

然后,替换

 ICollection<Tag> Tags {set;get}

在你的笔记类中

 ICollection<NoteTag> NoteTags {set;get}

以及在标签类中:

ICollection<Note> Notes {set;get;}

ICollection<NoteTags> NoteTags {set;get}

然后重写 DbContext 中的 OnModelCreating 方法:

protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<NoteTag>()
                .HasKey(t => new { t.NoteId, t.TagId });

            modelBuilder.Entity<NoteTag>()
                .HasOne(pt => pt.Note)
                .WithMany(p => p.NoteTags)
                .HasForeignKey(pt => pt.NoteId);

            modelBuilder.Entity<NoteTag>()
                .HasOne(pt => pt.Tag)
                .WithMany(t => t.NoteTags)
                .HasForeignKey(pt => pt.TagId);
        }

【讨论】:

  • 谢谢,迁移成功了。但是很奇怪,我敢肯定我之前在实体框架中可以做多对多的关系
  • @Ludwik11 在 EF6 中是,但不是 EF7,也只是偶然发现了这个错误,并想为什么我在这里有另一个 N 到 M 工作,然后我意识到它的两个 1 到 N 有一个桥接表,因为table 有一个额外的属性!
【解决方案2】:

我使用的是 EF 7,这个问题在我周末的大约 2 个小时内解决。 :) 所以,这是一个简单的解决方案 - 我有一个像这样的个人资料课程-

[Table("Profile")]
public class Profile
{
    public Profile()
    {

    }

    [Column(Order = 1)]
    [Key]        
    public Guid ProfileID { get; set; }
    [JsonIgnore]
    public virtual ICollection<StudentLivingWith> StudentProfileMap { get; set; }

    [JsonIgnore]
    public virtual ICollection<StudentLivingWith> ParentProfileMap { get; set; }
}

我在另一个名为“StudentLivingWith”的表中使用 ProfileID 作为 F 键引用。 (是的,我知道这个名字有点奇怪。:))正如你在下面的课程中看到的那样,“StudentProfileID”和“ParentProfileID”这两个列都引用了我的“Profile”表的同一列“profileID”。

[Table("StudentLivingWith")]
public class StudentLivingWith
{
    public StudentLivingWith()
    {

    }

    [Column(Order = 1)]
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int StudentLivingWithID { get; set; }

    [Column(Order = 2)]
    [ForeignKey("StudentProfileID")]
    public Guid StudentProfileID { get; set; }

    [Column(Order = 3)]
    [ForeignKey("ParentProfileID")]
    public Guid ParentProfileID { get; set; }

    [JsonIgnore]
    [InverseProperty("StudentProfileMap")]
    public virtual ICollection<Profile> StudentProfile { get; set; }

    [JsonIgnore]
    [InverseProperty("ParentProfileMap")]
    public virtual ICollection<Profile> ParentProfile { get; set; }
}    

所以结论是——你只需要在引用上添加 [InverseProperty] 标签,这个简单的解决方案就为我解决了问题。

我希望这会有所帮助。谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-06
    • 1970-01-01
    • 2020-09-27
    • 1970-01-01
    • 2019-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多