C# 实体类型上的导航尚未添加到模型中,或已忽略,或已忽略entityType

C# 实体类型上的导航尚未添加到模型中,或已忽略,或已忽略entityType,c#,entity-framework,asp.net-core,dnx,entity-framework-core,C#,Entity Framework,Asp.net Core,Dnx,Entity Framework Core,实体类型“Notepad.Models.Note”上的导航“Tags”尚未添加到模型中,或被忽略,或entityType被忽略 公共课堂笔记 { 公共说明() { CreationDate=日期时间。现在; Tags=newhashset(); Parts=新HashSet(); } 公共int ID{get;set;} 公共虚拟ICollection标记{get;set;} 公共虚拟ICollection部分{get;set;} 公共日期时间?CreationDate{get;set;} }

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

公共课堂笔记
{
公共说明()
{
CreationDate=日期时间。现在;
Tags=newhashset();
Parts=新HashSet();
}
公共int ID{get;set;}
公共虚拟ICollection标记{get;set;}
公共虚拟ICollection部分{get;set;}
公共日期时间?CreationDate{get;set;}
}
公共类标签
{
公共标签()
{
Notes=新的HashSet();
}
公共int ID{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection注释{get;set;}
}
添加迁移时会发生这种情况:

dnx ef迁移添加DbData-c DataDbContext

你认为这是为什么

编辑: DataDbContext:

public class DataDbContext : DbContext
    {
        public DbSet<Note> Notes { get; set; }
        public DbSet<Tag> Tags { get; set; }
        public DbSet<Part> Parts { get; set; }
    }
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);
        }
公共类DataDbContext:DbContext
{
公共DbSet注释{get;set;}
公共DbSet标记{get;set;}
公共DbSet部分{get;set;}
}

您在那里有多对多关系。正如文件所说:

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

因此,您必须创建额外的“join”类,如下所示:

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标记{set;get}
在你的笔记课上

 ICollection<NoteTag> NoteTags {set;get}
ICollection NoteTags{set;get}
并且在标记类中:

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

ICollection NoteTags{set;get}
然后在DbContext中重写OnModelCreating方法:

public class DataDbContext : DbContext
    {
        public DbSet<Note> Notes { get; set; }
        public DbSet<Tag> Tags { get; set; }
        public DbSet<Part> Parts { get; set; }
    }
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);
        }
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
modelBuilder.Entity()
.HasKey(t=>new{t.NoteId,t.TagId});
modelBuilder.Entity()
.HasOne(pt=>pt.Note)
.WithMany(p=>p.notetag)
.HasForeignKey(pt=>pt.NoteId);
modelBuilder.Entity()
.HasOne(pt=>pt.Tag)
.WithMany(t=>t.notetag)
.HasForeignKey(pt=>pt.TagId);
}

我正在使用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; }
}
[表格(“档案”)]
公共班级简介
{
公众简介()
{
}
[第列(顺序=1)]
[关键]
公共Guid配置文件ID{get;set;}
[JsonIgnore]
公共虚拟ICollection StudentProfileMap{get;set;}
[JsonIgnore]
公共虚拟ICollection ParentProfileMap{get;set;}
}
我在另一个名为“StudentLivingWith”的表中使用ProfileID作为F键引用。(是的,我知道这个名字有点奇怪。:)正如你在下面的课堂上看到的,“StudentProfileID”和“ParentProfileID”两列都指向我的“Profile”表中的同一列“profileID”

[表格(“学生生活”)]
公共班级学生生活在一起
{
公共学生与……生活在一起
{
}
[第列(顺序=1)]
[关键]
[数据库生成(DatabaseGeneratedOption.Identity)]
公共int StudentLivingWithID{get;set;}
[第列(顺序=2)]
[ForeignKey(“StudentProfileID”)]
公共Guid StudentProfileID{get;set;}
[第列(顺序=3)]
[ForeignKey(“ParentProfileID”)]
公共Guid ParentProfileID{get;set;}
[JsonIgnore]
[反向属性(“学生档案图”)]
公共虚拟ICollection学生档案{get;set;}
[JsonIgnore]
[InverseProperty(“ParentProfileMap”)]
公共虚拟ICollection ParentProfile{get;set;}
}    
因此,结论是-您只需要在引用上添加[InverseProperty]标记,这个简单的解决方案帮了我的忙


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

显示
标记的代码
您是否已将DbSet添加到DataDbContext?@MarcinZablocki我已将其粘贴到问题中,您可以查看是否所有内容都正确谢谢,迁移似乎已成功完成。但这是非常奇怪的,我相信我可以做很多对很多的关系之前,在实体framework@Ludwik11是的,在EF6中,但不是EF7,也只是被这个错误难住了,我想为什么我这里有另一个N到M工作,然后我意识到它的两个1到N和一个桥接表,因为该表有一个额外的属性!
[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; }
}