多对多EF7

编程入门 行业动态 更新时间:2024-10-25 20:17:17
本文介绍了多对多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

本文发布于:2023-11-15 14:26:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1595241.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!