![](/img/trans.png)
[英]Cannot execute EF Core migration on remote Azure SQL Server database - No such host is known
[英]NullReferenceException on SaveChangesAsync() in EF Core with SQL database in Azure
我搜索了這個問題,與流行的答案不同,我沒有一個與類同名的視圖。 這是拋出異常的地方:
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()
我嘗試刪除和讀取遷移、更新數據庫等。
當實體框架從其內部類的深處拋出隨機的、無法理解的異常時,通常是線程問題。 DbContext 不是線程安全的。 您不能從多個線程添加實體。
來自評論:
看起來我忘了添加調用 AddPatientAsync 方法的等待。 也許這就是為什么它是多線程的
如果您不等待對AddPatientAsync()
調用並在循環中調用它,則多個線程正在訪問相同的_context
。 你不能那樣做; 等待電話。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.