【发布时间】:2020-05-28 22:59:11
【问题描述】:
我有问题。我有 ASP .NET Core REST API 应用程序,并且在一种方法中,我试图将更多更改异步写入数据库。每次将不同数量的对象写入数据库时,每次出现三种不同错误之一。任何建议可以错了吗?
这是我的代码:
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
...
services.AddDbContext<MyDbContext>(options => options.UseSqlServer(connection_string), ServiceLifetime.Transient);
services.AddScoped<IHelper, Helper>();
...
}
Helper.cs
private MyDbContext _dbContext;
public Helper(IOptions<HelperSettings> settings, ILogger<Helper> logger, MyDbContext dbContext)
{
...
_dbContext = dbContext;
...
}
public void Save(object entity)
{
...
_dbContext.Add(entity);
}
这是引发异常的控制器和方法。
public class MyController : ControllerBase
{
private readonly Helper _db;
public MyController(IHelper helper)
{
_db = helper;
}
...
[HttpPost]
[Route("something")]
[Produces("application/json")]
public async Task<ActionResult<Guid>> CreateSomethingAsync([FromBody] DataRequest data)
{
...
if (data.Answers != null)
{
List<Task> saveTasks = new List<Task>();
foreach (AnswerData ans in data.Answers)
{
Answer answer = ans.ConvertToAnswer(); //just create new Answer instance and filll it with data from AnswerData
saveTasks.Add(Task.Run(() => _db.Save(answer)));
}
await Task.WhenAll(saveTasks);
await _db.DbContext.SaveChangesAsync();
}
return Ok(...);
}
}
我在另一个应用程序中循环调用CreateSomethingAsync()。它会引发以下三个异常之一:
System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'
或
System.InvalidOperationException: 'Operations that change non-concurrent collections must have exclusive access. A concurrent update was performed on this collection and corrupted its state. The collection's state is no longer correct.'
或
System.InvalidOperationException: Cannot start tracking InternalEntityEntry for entity type 'Answer' because another InternalEntityEntry is already tracking the same entity
在线_dbContext.Add(entity);我的Helper.cs。
我知道问题出在并行性上,但我不知道如何解决。有什么想法吗?
【问题讨论】:
-
你为什么在
Task.Run里面打电话给_db.Save?DbContext不是线程安全的。 -
Add不对数据库做任何事情,它只是在上下文中添加一个 entity 以进行跟踪。SaveChangesAsync保留所有更改。这是一件GOOD 的事情 - 它为您提供工作单元、断开连接的操作、只需不 调用SaveChanges即可回滚更改的能力。这个Save方法没有任何作用,也不需要在后台调用。这是瞬间的 -
我怀疑整个
Helper类是试图实现一个“存储库”,EF 本身已经提供了一些东西 - DbSet 本质上是一个存储库
标签: c# .net entity-framework asp.net-core dependency-injection