我有一个SQL Server数据库,表中有500,000条记录main.还有其他三个表叫child1,child2和child3.很多之间一对多的关系child1,child2,child3,并main通过三个关系表来实现:main_child1_relationship,main_child2_relationship,和main_child3_relationship.我需要读取记录main,更新main,并在关系表中插入新行以及在子表中插入新记录.子表中的记录具有唯一性约束,因此实际计算的伪代码(CalculateDetails)将类似于:
for each record in main
{
find its child1 like qualities
for each one of its child1 qualities
{
find the record in child1 that matches that quality
if found
{
add a record to main_child1_relationship to connect the two records
}
else
{
create a new record in child1 for the …Run Code Online (Sandbox Code Playgroud) 我有像
public interface IEmployeeRepository
{
Task<EmployeeSettings> GetEmployeeSettings(int employeeId);
Task<ICollection<DepartmentWorkPosition>> GetWorkPositions(int employeeId);
}
Run Code Online (Sandbox Code Playgroud)
存储库的构造函数(DbContext 注入):
public EmployeeRepository(EmployeeDbContext dbContext)
{
_dbContext = dbContext;
}
Run Code Online (Sandbox Code Playgroud)
并在 EF Core 2.0 中调用它,例如
var settingsTask = _employeeRepository
.GetEmployeeSettings(employeeId.Value);
var workPositionsTask = _employeeRepository
.GetWorkPositions(employeeId.Value);
await Task.WhenAll(settingsTask, workPositionsTask);
// do other things...
Run Code Online (Sandbox Code Playgroud)
问题:
对于 EF Core 3.0,有 InvalidOperationException:在上一个操作完成之前,在此上下文上启动了第二个操作...
DbContext 在 ConfigureServices 中注册,如
services.AddDbContext<EmployeeDbContext>(ServiceLifetime.Transient);
Run Code Online (Sandbox Code Playgroud)
教程如下:Entity Framework Core 不支持在同一个 DbContext 实例上运行多个并行操作。
但!如何在异步存储库中使用它?