c# - Deleting large volumes of data from database using .NetCore
问题描述
I am trying to delete over 5000 records from the database using .netCore 2.1. I have the following method which works fine however it takes way too long.
public async Task<bool> deleteAdhocDetails(int[] id)
{
var status = false;
for (var x = 0; x < id.Length; x++)
{
var existingReward = await _context.AdhocRewardInfo
.Where(d => d.RowID == id[x])
.FirstOrDefaultAsync<AdhocRewardInfo>();
if ((existingReward != null) && (existingReward.HaloRewardCode != null))
{
try
{
//removing existingReward
_context.AdhocRewardInfo.Remove(existingReward);
await _context.SaveChangesAsync();
status = true;
}
catch (Exception e)
{
throw e;
}
}
}
return status;
}
I am currently using EFCore.BulkExtensions for inserting records and it works nicely. I tried using BulkDelete but it didn't seem to make a difference. I also tried to use Z.EntityFramework.Extensions.EFCore but couldn't get that to work too. I should also mention that I am kinda new to this. If someone could please point me in the right direction I would appreciate it. Thanks
In the adhoc.service.ts:
deleteAdhocRecipients(id: number[]): Promise<boolean> {
return this.http.put<boolean>(this.baseUrl + 'deleteAdhocDetails', id)
.toPromise();
}
and in the .ts file:
this.general
.load(this.aService.deleteAdhocCampaign(this.adhocForm.get('create.id').value))
.then(
y => {
if (deleteArr.length > 0) {
this.general.load(this.aService.deleteAdhocRecipients(deleteArr))
.then(
f => this.router.navigate(['/adhoc-campaign/lookup'])
);
} else {
this.router.navigate(['/adhoc-campaign/lookup']);
}
}
);
解决方案
您可以删除对象的选择,然后只做一个 saveChanges
public async Task<bool> deleteAdhocDetails((int id, object haloRewardCode)[] id)
{
try
{
for (var x = 0; x < id.Length; x++)
{
var existingReward = new AdhocRewardInfo() { id = id[x].id, HaloRewardCode = id[x].haloRewardCode };
if ((existingReward != null) && (existingReward.HaloRewardCode != null))
{
//removing existingReward
_context.AdhocRewardInfo.Remove(existingReward);
}
}
}
catch (Exception e)
{
throw;
}
await _context.SaveChangesAsync();
return true;
}
推荐阅读
- javascript - 从 localhost geoJSON 文件链接到本地文件目录
- c++ - htons 给网络时间增加了多少开销
- android - Recyclerview 项目不可见/键盘可见时丢失
- c# -
- java - 每条聊天消息我应该限制的最大字符数是多少?
- android - 使用一个动画对象进行多视图
- wpf - 用户控制器后台操作
- ubuntu - 从颤振 1.9 开始:如何在浏览器中运行颤振应用程序?Flutter run -d chrome 不工作
- javascript - 为什么 jquery-file 上传总是将单个文件发送到后端
- path - 如何将python模块路径设置为jupyter notebook