英文:
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<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();
}
Can use package Z entity frame work but it's not fully Open source. for now BulkInsert
is Free
package:<PackageReference Include="Z.EntityFramework.Plus.EFCore" Version="6.22.3" />
code as follows
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);
}
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论