如何在Entity Framework Core 6中使用C#执行SQL批量插入

huangapple go评论74阅读模式
英文:

How to perform SQL batch inserts in Entity Framework Core 6 using C#

问题

在EF Core 6中是否可以批量插入?

英文:

I have the following code to insert applications to a table:

public async Task<Application> InsertApplicationAsync(List<Application> applications)
{
     foreach (var app in applications)
     {
            var application = new Application();
            application.Id = Id;
            application.Identifier = Identifier;
            await _context.AddAsync(application);
            await _context.SaveChangesAsync();
     }
}

Is it possible to insert in bulk in EF Core 6?

答案1

得分: 1

使用AddRange一次性插入批量数据的代码如下所示。

public async Task<Application> InsertApplicationAsync(List<Application> applications)
{
    var Data = applications.Select(App => new Application
    {
        Id = App.Id,
        Name = App.Name,
        //.
        //.
        //.
        //....
    }).ToList();
    await _context.AddRangeAsync(Data);
    await _context.SaveChangesAsync();
}

可以使用Z Entity Framework包,但它不是完全开源的。目前,BulkInsert是免费的。

包:<PackageReference Include="Z.EntityFramework.Plus.EFCore" Version="6.22.3" />

代码如下所示。

public async Task<Application> InsertApplicationAsync(List<Application> applications)
{
    var Data = applications.Select(App => new Application
    {
        Id = App.Id,
        Name = App.Name,
        //.
        //.
        //.
        //....
    }).ToList();
    _context.BulkInsert(Data);
}
英文:

use AddRange to insert Bulk data at once code as follows.

   public async Task&lt;Application&gt; InsertApplicationAsync(List&lt;Application&gt; applications)
    {
        var Data = applications.Select(App =&gt; new Application
        {
            Id=App.Id,
            Name=App.Name,
            //.
            //.
            //.
            //....
        }).ToList();
        await _context.AddRangeAsync(Data);
        await _context.SaveChangesAsync();
    }

Can use package Z entity frame work but it's not fully Open source. for now BulkInsert is Free

package:&lt;PackageReference Include=&quot;Z.EntityFramework.Plus.EFCore&quot; Version=&quot;6.22.3&quot; /&gt;

code as follows

   public async Task&lt;Application&gt; InsertApplicationAsync(List&lt;Application&gt; applications)
    {
        var Data = applications.Select(App =&gt; new Application
        {
            Id = App.Id,
            Name = App.Name,
            //.
            //.
            //.
            //....
        }).ToList();
        _context.BulkInsert(Data);
    }

答案2

得分: 0

你可以使用 https://github.com/borisdj/EFCore.BulkExtensions

从文档中:

context.BulkInsert(entities);                 context.BulkInsertAsync(entities);
context.BulkInsertOrUpdate(entities);         context.BulkInsertOrUpdateAsync(entities);         // Upsert
context.BulkInsertOrUpdateOrDelete(entities); context.BulkInsertOrUpdateOrDeleteAsync(entities); // Sync
context.BulkUpdate(entities);                 context.BulkUpdateAsync(entities);
context.BulkDelete(entities);                 context.BulkDeleteAsync(entities);
context.BulkRead(entities);                   context.BulkReadAsync(entities);
context.BulkSaveChanges();                    context.BulkSaveChangesAsync();

查看使用示例:https://github.com/borisdj/EFCore.BulkExtensions/blob/master/EFCore.BulkExtensions.Tests/EFCoreBulkTestAsync.cs

英文:

You can use https://github.com/borisdj/EFCore.BulkExtensions

From docs:

context.BulkInsert(entities);                 context.BulkInsertAsync(entities);
context.BulkInsertOrUpdate(entities);         context.BulkInsertOrUpdateAsync(entities);         // Upsert
context.BulkInsertOrUpdateOrDelete(entities); context.BulkInsertOrUpdateOrDeleteAsync(entities); // Sync
context.BulkUpdate(entities);                 context.BulkUpdateAsync(entities);
context.BulkDelete(entities);                 context.BulkDeleteAsync(entities);
context.BulkRead(entities);                   context.BulkReadAsync(entities);
context.BulkSaveChanges();                    context.BulkSaveChangesAsync();

See usage example here: https://github.com/borisdj/EFCore.BulkExtensions/blob/master/EFCore.BulkExtensions.Tests/EFCoreBulkTestAsync.cs

huangapple
  • 本文由 发表于 2023年7月11日 08:13:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76658009.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定