多对多EF7

沙蒿

楷模:

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; }
}

使用EF6进行OnModelCreating

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"));
}

我使用SQLite。如何使用EF7做同样的事情?

尼加德

EF7的文档说明了如何实现:http ://docs.efproject.net/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; }
        }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章