class a{
list<b>
}
class b{
list<a>
}
class a_b{
aid;
bid;
}
entityframecore 目前只能这样完
https://docs.microsoft.com/en-us/ef/core/modeling/relationships
多对多要这么搞。。。
1 class MyContext : DbContext 2 { 3 public DbSet<Post> Posts { get; set; } 4 public DbSet<Tag> Tags { get; set; } 5 6 protected override void OnModelCreating(ModelBuilder modelBuilder) 7 { 8 modelBuilder.Entity<PostTag>() 9 .HasKey(t => new { t.PostId, t.TagId }); 10 11 modelBuilder.Entity<PostTag>() 12 .HasOne(pt => pt.Post) 13 .WithMany(p => p.PostTags) 14 .HasForeignKey(pt => pt.PostId); 15 16 modelBuilder.Entity<PostTag>() 17 .HasOne(pt => pt.Tag) 18 .WithMany(t => t.PostTags) 19 .HasForeignKey(pt => pt.TagId); 20 } 21 } 22 23 public class Post 24 { 25 public int PostId { get; set; } 26 public string Title { get; set; } 27 public string Content { get; set; } 28 29 public List<PostTag> PostTags { get; set; } 30 } 31 32 public class Tag 33 { 34 public string TagId { get; set; } 35 36 public List<PostTag> PostTags { get; set; } 37 } 38 39 public class PostTag 40 { 41 public int PostId { get; set; } 42 public Post Post { get; set; } 43 44 public string TagId { get; set; } 45 public Tag Tag { get; set; } 46 }