英文:
CreateAsync very slow on DbContent
问题
我正在使用.NET Core 3.1的EF
我有一个DbContext,保存3000个对象需要很长时间
这似乎越来越慢,进入循环的次数越多
有人遇到过这种情况吗?
我已经尝试过AddAsync和Add,没有任何区别
foreach (var item in itemsToSave)
{
await _myRepository.CreateNonThreadedAsync(item, false);
}
在我的仓库中....
public async Task CreateAsync(TEntity entity, bool isSaved = true)
{
await DbContext.Set
if (isSaved)
{
await SaveChangesAsync();
}
}
public async Task CreateNonThreadedAsync(TEntity entity, bool isSaved = true)
{
DbContext.Set
if (isSaved)
{
await SaveChangesAsync();
}
}
我必须启用更改跟踪,因为这是一个过程的一部分,在完成一切后我调用上下文的SaveChangesAsync
在此之外还有一段逻辑,我在那里调用SaveChangesAsync,所以这里没有
我尝试调用AddRangeAsync,但在调用SaveChangesAsync后没有保存任何内容,也没有错误
Paul
英文:
I am using EF for .NET Core 3.1
I have a DbContext which is taking ages to save 3000 objects
This seems to get slower the further into the loop
Has anyone had this?
I have tried AddAsync and Add with no difference
foreach (var item in itemsToSave)
{
await _myRepository.CreateNonThreadedAsync(item, false);
}
In my repository....
public async Task CreateAsync(TEntity entity, bool isSaved = true)
{
await DbContext.Set<TEntity>().AddAsync(entity);
if (isSaved)
{
await SaveChangesAsync();
}
}
public async Task CreateNonThreadedAsync(TEntity entity, bool isSaved = true)
{
DbContext.Set<TEntity>().Add(entity);
if (isSaved)
{
await SaveChangesAsync();
}
}
I have to turn on change tracking because this is part of a process where I call SaveChangesAsync on the context once everything is finished
There is another piece of logic outside of this where I call SaveChangesAsync so that is why it is not here
I tried calling AddRangeAsync but nothing gets saved after SaveChangesAsync has been called and there are no errors
Paul
答案1
得分: 2
> 我有一个 DbContext,保存 3000 个对象需要很长时间
逐个添加实体可能会对性能产生负面影响,可以使用 AddRange(Async)
来缓解,因此从创建一个接受实体集合的 Create
重载开始,将在内部确定如何批量处理请求:
public async Task CreateAsync(TEntity[] entities, bool isSaved = true)
{
await DbContext.Set<TEntity>().AddRangeAsync(entities); // 或只使用 AddRange
if (isSaved)
{
await SaveChangesAsync();
}
}
但总的来说,EF Core 不是批量插入的最佳工具,确保在代码中执行批量保存(即执行 3000 个单独的请求,并将 isSaved
设置为 true
)。
阅读更多:
英文:
> I have a DbContext which is taking ages to save 3000 objects
Adding entites by one can negatively affect the performance which can be mitigated with AddRange(Async)
, so start from creating a Create
overload which accepts a collection of entities and will determine internally how to batch the requests:
public async Task CreateAsync(TEntity[] entities, bool isSaved = true)
{
await DbContext.Set<TEntity>().AddRangeAsync(entities); // or just AddRange
if (isSaved)
{
await SaveChangesAsync();
}
}
But in general EF Core is not the best tool for batch inserts, and be sure that in your code you perform the batch saving (i.e. that you will perform 3000 separate requests and will wait for each one sequentially with isSaved
set to true
).
Read more:
答案2
得分: 1
Try this code
await DbContext.Set<TEntity>().AddRangeAsync(itemsToSave):
await SaveChangesAsync();
英文:
Try this code
await DbContext.Set<TEntity>().AddRangeAsync(itemsToSave):
await SaveChangesAsync();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论