You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Performing a data access operation within the ChangeTracker.Tracked event results in an InvalidOperationException being thrown due to the EF Core code being within a critical section.
We were really hoping to use this event to get around the lack of lifecycle hooks (#626).
In our specific case, we are porting a codebase from LINQ to SQL to EF Core. This codebase extensively used the LINQ to SQL OnLoaded() partial implementations.
Is there any chance that the Tracked event could be modified to allow data access to occur within the event delegate?
Exception message: A second operation started on this context before a previous operation completed. Any instance members are not guaranteed to be thread safe.
Stack trace:
at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.Load[TSource](IQueryable`1 source)
at Microsoft.EntityFrameworkCore.Internal.EntityFinder`1.Load(INavigation navigation, InternalEntityEntry entry)
at Microsoft.EntityFrameworkCore.ChangeTracking.NavigationEntry.Load()
at Microsoft.EntityFrameworkCore.ChangeTracking.CollectionEntry.Load()
at Microsoft.EntityFrameworkCore.Internal.LazyLoader.Load(Object entity, String navigationName)
at Microsoft.EntityFrameworkCore.Proxies.Internal.LazyLoadingInterceptor.Intercept(IInvocation invocation)
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Castle.Proxies.EmployeeProxy.get_Devices()
at EFCore_TrackedEventCriticalSection.TestDataContext.ChangeTracker_Tracked(Object sender, EntityTrackedEventArgs e) in C:\Users\Michael\Code\EFCore-TrackedEventCriticalSection\DataContext.cs:line 18
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.OnTracked(InternalEntityEntry internalEntityEntry, Boolean fromQuery)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.MarkUnchangedFromQuery(ISet`1 handledForeignKeys)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.StartTrackingFromQuery(IEntityType baseEntityType, Object entity, ValueBuffer& valueBuffer, ISet`1 handledForeignKeys)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityTrackingInfo.StartTracking(IStateManager stateManager, Object entity, ValueBuffer& valueBuffer)
at Microsoft.EntityFrameworkCore.Query.QueryContext.StartTracking(Object entity, EntityTrackingInfo entityTrackingInfo)
at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.<_TrackEntities>d__17`2.MoveNext()
at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at EFCore_TrackedEventCriticalSection.Program.Main(String[] args) in C:\Users\Michael\Code\EFCore-TrackedEventCriticalSection\Program.cs:line 21</StackTrace><ExceptionString>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.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.Load[TSource](IQueryable`1 source)
at Microsoft.EntityFrameworkCore.Internal.EntityFinder`1.Load(INavigation navigation, InternalEntityEntry entry)
at Microsoft.EntityFrameworkCore.ChangeTracking.NavigationEntry.Load()
at Microsoft.EntityFrameworkCore.ChangeTracking.CollectionEntry.Load()
at Microsoft.EntityFrameworkCore.Internal.LazyLoader.Load(Object entity, String navigationName)
at Microsoft.EntityFrameworkCore.Proxies.Internal.LazyLoadingInterceptor.Intercept(IInvocation invocation)
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Castle.Proxies.EmployeeProxy.get_Devices()
at EFCore_TrackedEventCriticalSection.TestDataContext.ChangeTracker_Tracked(Object sender, EntityTrackedEventArgs e) in C:\Users\Michael\Code\EFCore-TrackedEventCriticalSection\DataContext.cs:line 18
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.OnTracked(InternalEntityEntry internalEntityEntry, Boolean fromQuery)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.MarkUnchangedFromQuery(ISet`1 handledForeignKeys)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.StartTrackingFromQuery(IEntityType baseEntityType, Object entity, ValueBuffer&amp; valueBuffer, ISet`1 handledForeignKeys)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityTrackingInfo.StartTracking(IStateManager stateManager, Object entity, ValueBuffer&amp; valueBuffer)
at Microsoft.EntityFrameworkCore.Query.QueryContext.StartTracking(Object entity, EntityTrackingInfo entityTrackingInfo)
at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.&lt;_TrackEntities&gt;d__17`2.MoveNext()
at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at EFCore_TrackedEventCriticalSection.Program.Main(String[] args) in C:\Users\Michael\Code\EFCore-TrackedEventCriticalSection\Program.cs:line 21</ExceptionString></Exception></TraceRecord>
An unhandled exception of type 'System.InvalidOperationException' occurred in Microsoft.EntityFrameworkCore.dll
A second operation started on this context before a previous operation completed. Any instance members are not guaranteed to be thread safe.
@divega As discussed on Skype, this is the issue we hit when using the Tracked event as a workaround for the lack of an ObjectMaterialized/OnLoaded type event (further discussion regarding this in #626).
We're currently working around this via custom Enumerator/IQueryProvider implementations, however we're not overly happy with it.
Performing a data access operation within the
ChangeTracker.Tracked
event results in anInvalidOperationException
being thrown due to the EF Core code being within a critical section.We were really hoping to use this event to get around the lack of lifecycle hooks (#626).
In our specific case, we are porting a codebase from LINQ to SQL to EF Core. This codebase extensively used the LINQ to SQL OnLoaded() partial implementations.
Is there any chance that the Tracked event could be modified to allow data access to occur within the event delegate?
Steps to reproduce
Repro included at https://github.com/optiks/EFCore-TrackedEventCriticalSection.
At a high level:
ChangeTracker.Tracked
eventFurther technical details
EF Core version: 2.1
Database Provider: EntityFrameworkCore.SqlServerCompact35
Operating system: Windows 10.0.17134.112
IDE: Visual Studio 2017 15.7.2
The text was updated successfully, but these errors were encountered: