gpt4 book ai didi

c# - 多对多 EF7

转载 作者:太空狗 更新时间:2023-10-29 17:39:06 26 4
gpt4 key购买 nike

模型:

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

我使用 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; }
}

关于c# - 多对多 EF7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28382959/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com