我有一个 ASP.NET Core 5 MVC Web 应用程序,使用 Entity Framework Core 5。
我们实现 Web 组件,例如网格或电子表格(我们与 Telerik 合作)。当我在组件中进行一些更改,然后尝试保存更改时,组件会调用我的ApplicationDbContext.SaveChanges
。然后我收到以下错误:
系统无效操作异常:配置的执行策略“SqlServerRetryingExecutionStrategy”不支持用户启动的事务。使用“DbContext.Database.CreateExecutionStrategy()”返回的执行策略将事务中的所有操作作为可重试单元执行。
这是我的方法ConfigureServices
对于数据库上下文:
public static void AddDbContext(this IServiceCollection services, string connectionString) =>
services.AddDbContext<ApplicationDbContext>(options =>
{
options.UseSqlServer(connectionString,
providerOptions =>
{
providerOptions
.EnableRetryOnFailure(
maxRetryCount: 5,
maxRetryDelay: TimeSpan.FromSeconds(30),
errorNumbersToAdd: null)
.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery);
});
options.EnableSensitiveDataLogging();
options.ConfigureWarnings(w => w.Throw(RelationalEventId.MultipleCollectionIncludeWarning));
});
该错误不会系统地发生。当然,当我尝试将多个元素保存到数据库时。而且当只有一个元素时也是如此。当我以经典形式逐一编辑或相同元素时不会发生,只有当它发生在多个对象场景(网格或电子表格)中时...
The StackTrace would be like this:
例如,我给出了一个允许提交的方法Entreprise
对象,从电子表格到数据库,以便使用CreateExecutionStrategy
method:
public ActionResult Data_Source_Entreprises_Submit(SpreadsheetSubmitViewModel model)
{
var result = new SpreadsheetSubmitViewModel()
{
Created = new List<SpreadsheetEntrepriseViewModel>(),
Updated = new List<SpreadsheetEntrepriseViewModel>(),
Destroyed = new List<SpreadsheetEntrepriseViewModel>()
};
if ((model.Created != null || model.Updated != null || model.Destroyed != null) && ModelState.IsValid)
{
/* executionStrategy */
var executionStrategy = _dbContext.Database.CreateExecutionStrategy();
executionStrategy.Execute(() =>
{
using (var transaction = _dbContext.Database.BeginTransaction())
{
try
{
///////// Business Logic //////////////////
if (model.Created != null)
{
foreach (var created in model.Created)
{
EntrepriseDTO dto = new EntrepriseDTO() { Nom = created.Nom };
var createdDto = _entrepriseService.Create(dto);
created.Id = createdDto.Id;
result.Created.Add(created);
}
}
if (model.Updated != null)
{
foreach (var updated in model.Updated)
{
var spec = new EntrepriseForDetailsSpecification(updated.Id);
var dto = _entrepriseService.Read(spec);
dto.Nom = updated.Nom;
_entrepriseService.Update(dto);
result.Updated.Add(updated);
}
}
if (model.Destroyed != null)
{
foreach (var destroyed in model.Destroyed)
{
_entrepriseService.Delete(destroyed.Id);
result.Destroyed.Add(destroyed);
}
}
///////// END Business Logic ///////////////
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
}
}
}
);
/* END executionStrategy */
return Json(result);
}
else
{
return StatusCode(400, "The models contain invalid property values.");
}
}
结果我遇到了以下错误:
连接不支持 MultipleActiveResultSets
然后我修改了连接字符串,包括MultipleActiveResultSets=True
,但最后又得到了另一个错误:
在上一个操作之前,在此上下文中启动了第二个操作
操作完成。这通常是由不同线程引起的
同时使用 DbContext 的同一实例。了解更多
有关如何避免 DbContext 的线程问题的信息,请参阅https://go.microsoft.com/fwlink/?linkid=2097913 https://go.microsoft.com/fwlink/?linkid=2097913.
我没有在该代码中使用异步代码,我不知道出了什么问题......