本文介绍了多对多EF7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
模型:
公共部分类电影 {公众诠释FilmID {搞定;组; } 公共虚拟的ICollection<&体裁GT;流派{搞定;组; } } 公共类体裁 {公众诠释GenreID {搞定;组; } 公共虚拟的ICollection<薄膜>电影{搞定;组; } }使用EF6
OnModelCreating
保护覆盖无效OnModelCreating(DbModelBuilder模型构建器) { modelBuilder.Entity<薄膜>() .HasMany(E => e。流派) .WithMany(E => e.Films) .MAP(M = GT; m.ToTable(Genre_Film)。MapLeftKey(Films_IdFilm)MapRightKey(Genres_IdGenre) ); }
我使用SQLite。 ?我该怎么办的使用EF7相同
解决方案有关EF7的文档说,这可怎么achived:的docs.efproject/en/latest/modeling/relationships.html#many-to -many
modelBuilder.Entity< PostTag>()。 HasOne(PT => pt.Post) .WithMany(p => p.PostTags) .HasForeignKey(PT => pt.PostId);modelBuilder.Entity< PostTag>() .HasOne(PT => pt.Tag) .WithMany(T => t.PostTags) .HasForeignKey(PT => pt.TagId); 公共类PostTag {公众诠释{帖子ID获取;组; } 公共文章张贴{搞定;组; } 公众诠释TAGID {搞定;组; } 公共标签标记{搞定;组; } }
Models:
public partial class Film { public int FilmID { get; set; } public virtual ICollection<Genre> Genres { get; set; } } public class Genre { public int GenreID { get; set; } public virtual ICollection<Film> Films { get; set; } }OnModelCreating using EF6
protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Film>() .HasMany(e => e.Genres) .WithMany(e => e.Films) .Map(m => m.ToTable("Genre_Film").MapLeftKey("Films_IdFilm").MapRightKey("Genres_IdGenre")); }I use SQLite. How I can do the same using EF7?
解决方案The docs for EF7 says how this can be achived: docs.efproject/en/latest/modeling/relationships.html#many-to-many
modelBuilder.Entity<PostTag>() .HasOne(pt => pt.Post) .WithMany(p => p.PostTags) .HasForeignKey(pt => pt.PostId);
modelBuilder.Entity<PostTag>() .HasOne(pt => pt.Tag) .WithMany(t => t.PostTags) .HasForeignKey(pt => pt.TagId); public class PostTag { public int PostId { get; set; } public Post Post { get; set; } public int TagId { get; set; } public Tag Tag { get; set; } }
更多推荐
多对多EF7
发布评论