1

我搜索了这个问题,与流行的答案不同,我没有同名的类的视图。这是引发异常的地方:

public async Task<Patient> AddPatientAsync(Patient newPatient)
{
    await _context.AddAsync(newPatient);
    await _context.SaveChangesAsync(); // exception

    return newPatient;
}

AddAsync方法有效,数据存储在 Azure 数据库中。但是该SaveChangesAsync方法会引发错误。

Patient模型是这样的:

public class Patient
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime DateOfBirth { get; set; }
    public Gender Gender { get; set; }
    public string ProtectedPin { get; set; }
    public IList<DoctorAppoint> DoctorAppoints { get; set; }
    public IList<NurseAppoint> NurseAppoints { get; set; }
    public IList<LabTest> LabTests { get; set; }
}

这是newPatient监视窗口中的对象:

在此处输入图像描述

这是堆栈跟踪:

at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityReferenceMap.Remove(Object entity, IEntityType entityType, EntityState oldState)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityReferenceMap.Update(InternalEntityEntry entry, EntityState state, Nullable`1 oldState)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.StateChanging(InternalEntityEntry entry, EntityState newState)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState oldState, EntityState newState, Boolean acceptChanges, Boolean modifyProperties)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState entityState, Boolean acceptChanges, Boolean modifyProperties, Nullable`1 forceStateWhenUnknownKey)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.AcceptChanges()
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.AcceptAllChanges(IReadOnlyList`1 changedEntries)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.<SaveChangesAsync>d__101.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.<ExecuteAsync>d__7`2.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Microsoft.EntityFrameworkCore.DbContext.<SaveChangesAsync>d__54.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at EchoBot.DBModels.Repositories.PatientRepository.<AddPatientAsync>d__3.MoveNext()

我尝试删除和读取迁移、更新数据库等。

4

1 回答 1

4

当实体框架从其内部类的深处抛出随机的、难以理解的异常时,通常是线程问题。DbContext 不是线程安全的。您不能从多个线程添加实体。

来自评论:

看起来我忘了添加一个调用 AddPatientAsync 方法的等待。也许这就是为什么它是多线程的

如果您不等待对您的AddPatientAsync()调用并在循环中调用它,则多个线程正在访问同一个_context. 你不能那样做;等待电话。

于 2020-09-28T11:06:30.510 回答