EFCore是否支持多个dbcontext以实现并发节省

时间:2017-07-27 02:11:04

标签: c# entity-framework .net-core entity-framework-core

我为数据库中的每个表使用单独的DbContext,它们分别在Task中独立运行以添加对象,并在任务中调用SaveChanges。有时抛出System.InvalidOperationException

2017-07-26 22:29:07.9670|ERROR|Microsoft.EntityFrameworkCore.DbContext|An exception occurred in the database while saving changes.
System.InvalidOperationException: A second operation started on this context before a previous operation completed. Any instance members are not guaranteed to be thread safe.

at Microsoft.EntityFrameworkCore.Internal.ConcurrencyDetector.EnterCriticalSection()
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.<SaveChangesAsync>d__54.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.<SaveChangesAsync>d__52.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.EntityFrameworkCore.DbContext.<SaveChangesAsync>d__35.MoveNext()

这是否意味着EFCore只支持数据库中的一个DbContext?

0 个答案:

没有答案