简体   繁体   English

在 Entity Framework Core 中保存多对多关系

[英]Saving many-to-many relationship in Entity Framework Core

For example, I have 3 classes, which I'm using for many-to-many relationship:例如,我有 3 个类,用于多对多关系:

public class Library
{
    [Key]
    public string LibraryId { get; set; }
    public List<Library2Book> Library2Books { get; set; }
}

public class Book
{
   [Key]
   public string BookId { get; set; }
   public List<Library2Book> Library2Books { get; set; }
}

public class Library2Book
{
    public string BookId { get; set; }
    public Book Book { get; set; }

    public string LibraryId { get; set; }
    public Library Library { get; set; }
}

They're configured in ApplicationDbContext :它们在ApplicationDbContext配置:

protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);
    builder.Entity<CodeableConcept2Coding>().HasKey(k => new { k.LibraryId, k.BookId });
    builder.Entity<Library2Book>()
        .HasOne(x => x.Library)
        .WithMany(x => x.Library2Book)
        .HasForeignKey(x => x.LibraryId);
    builder.Entity<Library2Book>()
        .HasOne(x => x.Book)
        .WithMany(x => x.Library2Book)
        .HasForeignKey(x => x.BookId);
}

So, I want to add to database some list of Library2Books :所以,我想向数据库添加一些Library2Books列表:

var library2Books = new List<Library2Books>(/*some sort of initialization*/);

What entity should I add first?我应该先添加什么实体? Books or maybe Library ? BooksLibrary How can I do this saving?我怎样才能做到这一点?

This is a simple and a very fundamental question to EF Core many-to-many relationship;这是 EF Core 多对多关系的一个简单且非常基本的问题; I do not know why no one has written a complete example for n..m in EF Core.我不知道为什么没有人在 EF Core 中为 n..m 编写完整的示例。

I have modified your code (primary key as int ), I do not like string in primary key.我已经修改了您的代码(主键为int ),我不喜欢主键中的字符串。 Just copy/paste the code and every should work fine.只需复制/粘贴代码,每个代码都可以正常工作。

What entity should I add first?我应该先添加什么实体? Books or maybe Library?书籍或图书馆? How can I do this saving?我怎样才能做到这一点?

The order is not important the important thing here is the data linking.顺序并不重要,这里重要的是数据链接。 The data must be correctly linked, see the comments between my code lines.数据必须正确链接,请参阅我的代码行之间的注释。

Notes:注意事项:

  • Many-to-many relationships without an entity class to represent the join table are not yet supported!尚不支持没有实体类来表示连接表的多对多关系! You must have a join table.你必须有一个连接表。

  • Many-to-many relationships consists of 2 separate one-to-many relationships.多对多关系由 2 个独立的一对多关系组成。 = 2x 1:N = 2x 1:N

     class Program { public class Library { [Key] public int LibraryId { get; set; } public List<Library2Book> Library2Books { get; set; } = new List<Library2Book>(); } public class Book { [Key] public int BookId { get; set; } public List<Library2Book> Library2Books { get; set; } = new List<Library2Book>(); } public class Library2Book { [Key] public int BookId { get; set; } public Book Book { get; set; } [Key] public int LibraryId { get; set; } public Library Library { get; set; } } public class MyDbContext : DbContext { public DbSet<Book> Books { get; set; } public DbSet<Library> Libraries { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer(@"Server=.\\;Database=EFTutorial;integrated security=True;"); base.OnConfiguring(optionsBuilder); } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Library2Book>().HasKey(k => new { k.LibraryId, k.BookId }); modelBuilder.Entity<Library2Book>() .HasOne(x => x.Book) .WithMany(x => x.Library2Books) .HasForeignKey(x => x.BookId); modelBuilder.Entity<Library2Book>() .HasOne(x => x.Library) .WithMany(x => x.Library2Books) .HasForeignKey(x => x.LibraryId); base.OnModelCreating(modelBuilder); } } static void Main(string[] args) { using (var myDb = new MyDbContext()) { // Create Db myDb.Database.EnsureCreated(); // I will add two books to one library var book1 = new Book(); var book2 = new Book(); // I create the library var lib = new Library(); // I create two Library2Book which I need them // To map between the books and the library var b2lib1 = new Library2Book(); var b2lib2 = new Library2Book(); // Mapping the first book to the library. // Changed b2lib2.Library to b2lib1.Library b2lib1.Book = book1; b2lib1.Library = lib; // I map the second book to the library. b2lib2.Book = book2; b2lib2.Library = lib; // Linking the books (Library2Book table) to the library lib.Library2Books.Add(b2lib1); lib.Library2Books.Add(b2lib2); // Adding the data to the DbContext. myDb.Libraries.Add(lib); myDb.Books.Add(book1); myDb.Books.Add(book2); // Save the changes and everything should be working! myDb.SaveChanges(); } } }

Results结果

Tables:   Books    |   Libraries      |    Library2Book  |
           1       |      1           |      1   |   1   |
           2       |      -           |      1   |   2   |

Edit from author of the question问题作者的编辑

When you're trying to insert a lot of entities (I've tried approximately 300), you'll have the same key has been already added error.当您尝试插入大量实体时(我已经尝试了大约 300 个),您将遇到相同的键已添加错误。 You should split your inserting collection into small parts, eg 100 entities should be enough.您应该将您的插入集合分成小部分,例如 100 个实体就足够了。

public async Task SaveEntities(IEnumerable<Library2Book> library2Books)
        {
                int i = 0;
                foreach (var library2Book in library2Books)
                {
                    _dbContext.Set<Library>().Add(codConc2Coding.Library);
                    _dbContext.Set<Book>().Add(codConc2Coding.Book);
                    _dbContext.Set<Library2Book>().Add(library2Book);
                    i++;
                    if (i == 99)
                    {
                        await _dbContext.SaveChangesAsync();
                        i = 0;
                    }
                }
                await _dbContext.SaveChangesAsync();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架核心:与同一实体的多对多关系 - Entity Framework Core: many-to-many relationship with same entity 与Entity Framework保存多对多关系到MySQL - Saving Many-To-Many Relationship with Entity Framework to MySQL 实体框架,以多对多关系发布保存数据 - Entity framework, issue saving data in many-to-many relationship 实体框架核心多对多关系未从数据库加载 - Entity Framework Core Many-to-Many Relationship not loading from database 实体框架核心与IdentityUser的多对多关系 - Entity Framework Core Many-to-Many relationship with IdentityUser 在Entity Framework Core中使用具有多对多关系的列表 - Using Lists with Many-to-Many relationship in Entity Framework Core Entity Framework Core 中的多对多关系错误 - Error with many-to-many relationship in Entity Framework Core 使用Entity Framework Core以多对多关系为数据库播种 - Seeding the database with a many-to-many relationship using Entity Framework Core Entity Framework Core:同一个对象的多对多关系 - Entity Framework Core: many-to-many relationship with the same object 实体框架4-具有视图的多对多关系 - Entity Framework 4 - Many-To-Many Relationship with a view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM