实体类型的导航尚未添加到模型中,或被忽略,或entityType被忽略 [英] The navigation on entity type has not been added to the model, or ignored, or entityType ignored
问题描述
实体类型Notepad.Models.Note上的导航Tags尚未添加到模型中,或被忽略,或entityType被忽略。
< blockquote>
public class Note
{
public Note()
{
CreationDate = DateTime 。现在;
标签= new HashSet< Tag>();
Parts = new HashSet< Part>();
}
public int ID {get;组; }
public virtual ICollection< Tag>标签{get;组; }
public virtual ICollection< Part>零件{get;组; }
public DateTime? CreationDate {get;组;
public class标签
{
public Tag()
{
Notes = new HashSet< Note> ();
}
public int ID {get;组; }
public string Name {get;组; }
public virtual ICollection< Note>注意{get;组; }
}
添加迁移时会发生:
dnx ef migrations添加DbData -c DataDbContext
你为什么认为发生?
编辑:
DataDbContext:public DataDbContext类:DbContext
{
public DbSet< Note>注意{get;组; }
public DbSet< Tag>标签{get;组; }
public DbSet< Part>零件{get;组;
}
解决方案你有多对多的关系在那里。正如文档所述: http://docs.efproject.net/en/最新/建模/ relationships.html#id21
尚未支持没有实体类来表示连接表的多对多关系。但是,您可以通过为连接表包含实体类并映射两个单独的一对多关系来表示多对多关系。
所以你必须创建这样一个额外的加入类:
public class NoteTag
{
public int NoteId {get;组; }
public注意注意{get;组; }
public int TagId {get;组; }
public Tag Tag {get;组; }
}
然后替换
ICollection< Tag>您的Note类中的标签{set; get}
ICollection< NoteTag> NoteTags {set; get}
以及Tag类中的
ICollection<注释>注意{set; get;}
to
的ICollection< NoteTags> NoteTags {set; get}
然后在DbContext中覆盖OnModelCreating方法:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity< NoteTag>()
.HasKey => 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);
}
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; } }
It happens while adding a migration:
dnx ef migrations add DbData -c DataDbContext
Why do you think it happens?
EDIT: DataDbContext:
public class DataDbContext : DbContext { public DbSet<Note> Notes { get; set; } public DbSet<Tag> Tags { get; set; } public DbSet<Part> Parts { get; set; } }
解决方案You have Many-to-many relationship there. As the documentation says: http://docs.efproject.net/en/latest/modeling/relationships.html#id21
Many-to-many relationships without an entity class to represent the join table are not yet supported. However, you can represent a many-to-many relationship by including an entity class for the join table and mapping two separate one-to-many relationships.
So you must create additional "join" class like this:
public class NoteTag { public int NoteId { get; set; } public Note Note { get; set; } public int TagId { get; set; } public Tag Tag { get; set; } }
then, replace
ICollection<Tag> Tags {set;get}
in your Note class to
ICollection<NoteTag> NoteTags {set;get}
and also in Tag class:
ICollection<Note> Notes {set;get;}
to
ICollection<NoteTags> NoteTags {set;get}
and then override OnModelCreating method in DbContext:
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); }
这篇关于实体类型的导航尚未添加到模型中,或被忽略,或entityType被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!