上的实体类型的导航尚未添加到模型,或忽略,或的EntityType忽略 [英] The navigation on entity type has not been added to the model, or ignored, or entityType ignored
问题描述
导航上的实体类型'标签'Notepad.Models.Note尚未添加到模型,或者忽视,或者被忽略的EntityType
公共类注
{
公众注意()
{
CreationDate =日期时间。现在;
标签=新的HashSet<吊牌及GT;();
零件=新的HashSet<部分>();
}
公众诠释ID {搞定;组; }
公共虚拟的ICollection<标记和GT;标签{搞定;组; }
公共虚拟的ICollection<部分>部分{搞定;组; }
公众的DateTime? CreationDate {搞定;组; }
}
公共类标签
{
公共标签()
{
注意=新的HashSet<注意> ();
}
公众诠释ID {搞定;组; }
公共字符串名称{;组; }
公共虚拟的ICollection<注意>注意{搞定;组; }
}
这同时增加了迁移发生了:
DNX EF migration增加DbData -c DataDbContext
块引用>
为什么你认为它发生
编辑:
DataDbContext:公开类DataDbContext:的DbContext
{
公共DbSet<注意>注意{搞定;组; }
公共DbSet<标记和GT;标签{搞定;组; }
公共DbSet<部分>部分{搞定;组; }
}
解决方案您的许多一对多关系存在。由于文件说: http://docs.efproject.net/en/latest/modeling/relationships html的#ID21
的没有一个实体类的许多一对多关系,表示联接表尚不支持。但是,您可以通过包含一个实体类的连接表和测绘两个分开的一对许多关系代表了许多一对多的关系。的
所以,你必须创建这样的额外的加盟类:
公共类NoteTag
{
公INT NoteId {搞定;组; }
公众注意注意{搞定;组; }
公众诠释TAGID {搞定;组; }
公共标签标记{搞定;组; }
}
然后更换
的ICollection<标记和GT;标签{集;获取}
在您的注意类
的ICollection< NoteTag> NoteTags {集;获取}
和也Tag类:
的ICollection<注意>注意{集;获取;}
到
的ICollection< NoteTags> NoteTags {集;获取}
,然后覆盖OnModelCreating方法的DbContext:
保护覆盖无效OnModelCreating(模型构建器模型构建器)
{
modelBuilder.Entity< NoteTag>()
.HasKey(T = gt;新建{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屋!