我有几个小时试图知道为什么在我致电SaveChangesAsync();
时会出现以下异常类型
mscorlib.dll中的System.Threading.ThreadAbortException'
我先使用EF6代码,以下是我的EntityModel
public class Visit
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[ForeignKey("User")]
public int UserId { get; set; }
[ForeignKey("Patient")]
public int PatientId { get; set; }
[ForeignKey("Doctor")]
public int DoctorId { get; set; }
[ForeignKey("VisitType")]
public int VisitTypeId { get; set; }
public DateTime VisitDate { get; set; }
public TimeSpan ArrivalTime { get; set; }
public TimeSpan? EnterToTestTime { get; set; }
[ForeignKey("VisitStatus")]
public int StatusId { get; set; }
public TimeSpan? CancelingTime { get; set; }
[MaxLength(500)]
public string CancelingReason { get; set; }
/// <summary>
/// Navigation Prop.
/// </summary>
public virtual User User { get; set; }
public virtual Patient Patient { get; set; }
public virtual Doctor Doctor { get; set; }
public virtual VisitType VisitType { get; set; }
public virtual VisitStatus VisitStatus { get; set; }
/// <summary>
/// Inverse Prop.
/// </summary>
public virtual ICollection<VisitNote> VisitNotes { get; set; }
}
以下是我的服务层类中的函数:
public async Task<bool> AddNewPatientVisit(IVisitModel vm)
{
using (var transaction = _unitOfWork.BeginTrainsaction())
{
try
{
var visit = new Visit
{
UserId = vm.UserId,
DoctorId = vm.DoctorId,
PatientId = vm.PatientId,
StatusId = vm.StatusId,
VisitDate = DateTime.Now.Date,
ArrivalTime = DateTime.Now.TimeOfDay,
VisitTypeId = vm.VisitTypeId,
};
await _unitOfWork.Visits.Insert(visit);
await _unitOfWork.Save();
transaction.Commit();
}
catch (Exception e)
{
transaction.Rollback();
return false;
}
}
return true;
}
保存方法:
public async Task<int> Save()
{
try
{
return await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException e)
{
Console.WriteLine(e);
return -1;
}
catch (DbUpdateException e)
{
Console.WriteLine(e);
return -1;
}
catch (DbEntityValidationException e)
{
Console.WriteLine(e);
return -1;
}
catch (ObjectDisposedException e)
{
Console.WriteLine(e);
return -1;
}
catch (InvalidOperationException e)
{
Console.WriteLine(e);
return -1;
}
catch (Exception e)
{
Console.WriteLine(e);
return -1;
}
}
具有堆栈跟踪
at System.Utf8String.HashCaseInsensitive(Void* sz, Int32 cSz)
at System.RuntimeType.RuntimeTypeCache.MemberInfoCache`1.GetListByName(Char* pName, Int32 cNameLen, Byte* pUtf8Name, Int32 cUtf8Name, MemberListType listType, CacheType cacheType)
at System.RuntimeType.RuntimeTypeCache.MemberInfoCache`1.Populate(String name, MemberListType listType, CacheType cacheType)
at System.RuntimeType.RuntimeTypeCache.MemberInfoCache`1.GetMemberList(MemberListType listType, String name, CacheType cacheType)
at System.RuntimeType.GetMethodCandidates(String name, BindingFlags bindingAttr, CallingConventions callConv, Type[] types, Boolean allowPrefixLookup)
at System.RuntimeType.GetMethodImpl(String name, BindingFlags bindingAttr, Binder binder, CallingConventions callConv, Type[] types, ParameterModifier[] modifiers)
at System.Type.GetMethod(String name)
at System.Linq.Expressions.Expression.ValidateLambdaArgs(Type delegateType, Expression& body, ReadOnlyCollection`1 parameters)
at System.Linq.Expressions.Expression.Lambda[TDelegate](Expression body, String name, Boolean tailCall, IEnumerable`1 parameters)
at System.Data.Entity.Core.Objects.DelegateFactory.CreatePropertySetter(Type entityDeclaringType, PropertyInfo propertyInfo, Boolean allowNull)
at System.Data.Entity.Core.Objects.DelegateFactory.GetSetterDelegateForProperty(EdmProperty property)
at System.Data.Entity.Core.Objects.DelegateFactory.SetValue(EdmProperty property, Object target, Object value)
at System.Data.Entity.Core.Objects.StateManagerMemberMetadata.SetValue(Object userObject, Object value)
at System.Data.Entity.Core.Objects.Internal.SnapshotChangeTrackingStrategy.SetCurrentValue(EntityEntry entry, StateManagerMemberMetadata member, Int32 ordinal, Object target, Object value)
at System.Data.Entity.Core.Objects.Internal.EntityWrapper`1.SetCurrentValue(EntityEntry entry, StateManagerMemberMetadata member, Int32 ordinal, Object target, Object value)
at System.Data.Entity.Core.Objects.EntityEntry.SetCurrentEntityValue(StateManagerTypeMetadata metadata, Int32 ordinal, Object userObject, Object newValue)
at System.Data.Entity.Core.Objects.ObjectStateEntryDbUpdatableDataRecord.SetRecordValue(Int32 ordinal, Object value)
at System.Data.Entity.Core.Mapping.Update.Internal.PropagatorResult.SetServerGenValue(Object value)
at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.BackPropagateServerGen(List`1 generatedValues)
at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.<UpdateAsync>d__0.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Data.Entity.Core.Objects.ObjectContext.<ExecuteInTransactionAsync>d__3d`1.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesToStoreAsync>d__39.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<ExecuteAsyncImplementation>d__9`1.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesInternalAsync>d__31.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at ECMS.DataAccess.Infrastructure.UnitOfWork.<Save>d__16.MoveNext() in C:\Users\Administrator\Documents\Visual Studio 2017\Projects\ECMS\ECMS.DataAccess\Infrastructure\UnitOfWork.cs:line 49