CreateAsync 在 DbContent 上非常慢

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

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().AddAsync(entity);
if (isSaved)
{
await SaveChangesAsync();
}
}

public async Task CreateNonThreadedAsync(TEntity entity, bool isSaved = true)
{
DbContext.Set().Add(entity);
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&lt;TEntity&gt;().AddRangeAsync(itemsToSave):
await SaveChangesAsync();

huangapple
  • 本文由 发表于 2023年6月26日 21:46:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76557286.html
匿名

发表评论

匿名网友

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

确定