C# 多对多EF7

C# 多对多EF7,c#,entity-framework-core,C#,Entity Framework Core,型号: 公共部分类电影 { public int FilmID{get;set;} 公共虚拟ICollection类型{get;set;} } 公共课体裁 { public int GenreID{get;set;} 公共虚拟ICollection电影{get;set;} } 使用EF6创建模型 public partial class Film { public int FilmID { get; set; } public virtual ICollection<Ge

型号:

公共部分类电影
{
public int FilmID{get;set;}
公共虚拟ICollection类型{get;set;}
}
公共课体裁
{
public int GenreID{get;set;}
公共虚拟ICollection电影{get;set;}
}
使用EF6创建模型

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; }
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity()
.HasMany(e=>e.Genres)
.有很多(e=>e.Films)
.Map(m=>m.ToTable(“电影类型”).MapLeftKey(“电影类型”).MapRightKey(“电影类型”));
}

我使用SQLite。如何使用EF7实现同样的功能?

映射API将在EF7中更改。有人提出了一个更广泛的方案。这里有一个关于多对多的简短词汇:

我们期望多对多API与一对多和一对一API非常相似

但是它还没有在当前的源代码中实现。在为测试创建的上下文中,它表示:

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"));
}
//待办事项:多对多
//modelBuilder.Entity().ForeignKey(fk=>fk.ForeignKey(e=>e.SupplierId));
这就是我所能找到的全部


我当然希望EF7在这方面能够向后兼容。

EF7的文档说明了如何实现这一点:

modelBuilder.Entity()
.HasOne(pt=>pt.Post)
.WithMany(p=>p.PostTags)
.HasForeignKey(pt=>pt.posted);
modelBuilder.Entity()
.HasOne(pt=>pt.Tag)
.WithMany(t=>t.posttag)
.HasForeignKey(pt=>pt.TagId);
公共类PostTag
{
公共int PostId{get;set;}
公共Post Post{get;set;}
public int TagId{get;set;}
公共标记{get;set;}
}
不知道您可以引用代码示例。很好!:)
// TODO: Many-to-many
//modelBuilder.Entity<TSupplier>().ForeignKeys(fk => fk.ForeignKey<TProduct>(e => e.SupplierId));
        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; }
        }