实体类型上的导航还没有被添加到模型中,或者被忽略,或者 entityType 被忽略
The navigation on entity type has not been added to the model, or ignored, or entityType ignored
The navigation 'Tags' on entity type 'Notepad.Models.Note' has not been added to the model, or ignored, or entityType ignored.
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 migrations add 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; }
}
你在那里有 Many-to-many 关系。正如文档所说:http://docs.efproject.net/en/latest/modeling/relationships.html#id21
尚不支持 Many-to-many 没有实体 class 来表示联接 table 的关系。但是,您可以通过为联接 table 包含实体 class 并映射两个单独的 one-to-many 关系来表示 many-to-many 关系。
因此您必须像这样创建额外的 "join" class:
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}
在你的笔记 class 到
ICollection<NoteTag> NoteTags {set;get}
还有标签 class:
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);
}
我正在使用 EF 7,这个问题花了我周末大约 2 个小时。 :)
所以,这是简单的解决方案 -
我的配置文件 class 是这样的 -
[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" 的 table 中使用 ProfileID 作为 F 键参考。 (是的,我知道这个名字有点奇怪。:))正如您在下面 class 中看到的,"StudentProfileID" 和 "ParentProfileID" 列都引用同一列 "profileID"我的 "Profile" table.
[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] 标签,这个简单的解决方案对我有用。
希望对您有所帮助。谢谢
The navigation 'Tags' on entity type 'Notepad.Models.Note' has not been added to the model, or ignored, or entityType ignored.
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 migrations add 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; }
}
你在那里有 Many-to-many 关系。正如文档所说:http://docs.efproject.net/en/latest/modeling/relationships.html#id21
尚不支持Many-to-many 没有实体 class 来表示联接 table 的关系。但是,您可以通过为联接 table 包含实体 class 并映射两个单独的 one-to-many 关系来表示 many-to-many 关系。
因此您必须像这样创建额外的 "join" class:
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}
在你的笔记 class 到
ICollection<NoteTag> NoteTags {set;get}
还有标签 class:
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);
}
我正在使用 EF 7,这个问题花了我周末大约 2 个小时。 :) 所以,这是简单的解决方案 - 我的配置文件 class 是这样的 -
[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" 的 table 中使用 ProfileID 作为 F 键参考。 (是的,我知道这个名字有点奇怪。:))正如您在下面 class 中看到的,"StudentProfileID" 和 "ParentProfileID" 列都引用同一列 "profileID"我的 "Profile" table.
[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] 标签,这个简单的解决方案对我有用。
希望对您有所帮助。谢谢