efcore: Query: Threading issues cause NullReferenceException in SimpleNullableDependentKeyValueFactory
I am getting random crash when getting the list but not sure why.
[HttpGet]
public async Task<List<BookingTimeSlotViewModel>> Get(DateTime start, DateTime end) // need period
{
User user = await GetCurrentUser();
// get booking slots with booking detail
IQueryable<BookingTimeSlot> slots;
if (await IsAmbassadorManager()) {
slots = _rosterRepository.GetBookingSlotForManagerBetween(user, start, end);
} else {
slots = _rosterRepository.GetBookingSlotForUserBetween(user, start, end);
}
return _mapper.Map<List<BookingTimeSlot>, List<BookingTimeSlotViewModel>>(await slots.ToListAsync()); // <<<<<<<<<< CRASH HERE
}
// repository
public IQueryable<BookingTimeSlot> GetBookingSlotForUserBetween(User user, DateTime start, DateTime end)
{
return GetBookingSlotBetween(start, end).Where(s => s.Booking.Outlet.Supervisor == user);
}
public IQueryable<BookingTimeSlot> GetBookingSlotBetween(DateTime start, DateTime end)
{
return _context.BookingTimeSlots
.Include(s => s.Booking.Outlet)
.Include(s => s.Rosters)
.Where(s => s.StartDate <= end && (s.EndDate >= start || (s.Repeat == true && s.RepeatUntil >= start)));
}
Here is the exception log:
---> (Inner Exception #0) System.AggregateException: One or more errors occurred. (One or more errors occurred. (Object reference not set to an instance of an object.)) ---> System.AggregateException: One or more errors occurred. (Object reference not set to an instance of an object.) ---> System.NullReferenceException: Object reference not set to an instance of an object.
at lambda_method(Closure , InternalEntityEntry )
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.SimpleNullableDependentKeyValueFactory`1.TryCreateFromCurrentValues(InternalEntityEntry entry, TKey& key)
at Microsoft.EntityFrameworkCore.Query.Internal.WeakReferenceIdentityMap`1.CreateIncludeKeyComparer(INavigation navigation, InternalEntityEntry entry)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryBuffer.IncludeCore(Object entity, INavigation navigation)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryBuffer.<IncludeAsync>d__13.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.EntityFrameworkCore.Query.Internal.AsyncGroupJoinInclude.<IncludeAsync>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.EntityFrameworkCore.Query.Internal.AsyncGroupJoinInclude.<IncludeAsync>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.EntityFrameworkCore.Query.AsyncQueryMethodProvider.GroupJoinAsyncEnumerable`4.GroupJoinAsyncEnumerator.<MoveNext>d__6.MoveNext()
--- End of inner exception stack trace ---
--- End of inner exception stack trace ---
---> (Inner Exception #0) System.AggregateException: One or more errors occurred. (Object reference not set to an instance of an object.) ---> System.NullReferenceException: Object reference not set to an instance of an object.
at lambda_method(Closure , InternalEntityEntry )
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.SimpleNullableDependentKeyValueFactory`1.TryCreateFromCurrentValues(InternalEntityEntry entry, TKey& key)
at Microsoft.EntityFrameworkCore.Query.Internal.WeakReferenceIdentityMap`1.CreateIncludeKeyComparer(INavigation navigation, InternalEntityEntry entry)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryBuffer.IncludeCore(Object entity, INavigation navigation)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryBuffer.<IncludeAsync>d__13.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.EntityFrameworkCore.Query.Internal.AsyncGroupJoinInclude.<IncludeAsync>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.EntityFrameworkCore.Query.Internal.AsyncGroupJoinInclude.<IncludeAsync>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.EntityFrameworkCore.Query.AsyncQueryMethodProvider.GroupJoinAsyncEnumerable`4.GroupJoinAsyncEnumerator.<MoveNext>d__6.MoveNext()
--- End of inner exception stack trace ---
---> (Inner Exception #0) System.NullReferenceException: Object reference not set to an instance of an object.
at lambda_method(Closure , InternalEntityEntry )
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.SimpleNullableDependentKeyValueFactory`1.TryCreateFromCurrentValues(InternalEntityEntry entry, TKey& key)
at Microsoft.EntityFrameworkCore.Query.Internal.WeakReferenceIdentityMap`1.CreateIncludeKeyComparer(INavigation navigation, InternalEntityEntry entry)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryBuffer.IncludeCore(Object entity, INavigation navigation)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryBuffer.<IncludeAsync>d__13.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.EntityFrameworkCore.Query.Internal.AsyncGroupJoinInclude.<IncludeAsync>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.EntityFrameworkCore.Query.Internal.AsyncGroupJoinInclude.<IncludeAsync>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.EntityFrameworkCore.Query.AsyncQueryMethodProvider.GroupJoinAsyncEnumerable`4.GroupJoinAsyncEnumerator.<MoveNext>d__6.MoveNext()<---
<---
<---
<---
<---
<---
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 25 (14 by maintainers)
I have found what causes the error in my project and created a new project that replicates the issue.
The problem occurs when the model has a one-to-many object (Post) that has a navigation property to a ‘parent’ (Blog), but that ‘many’ object (Post) does not specify a property BlogId in the model (see comment in BloggingContext.cs in project).
This in itself does not cause an error but a query which includes context.Posts.Where(x=> x.Blog.Id == 3) causes the null reference exception with repeated requests (but a single request does work).
project with issue: https://github.com/hallzhallz/dotnetcore/tree/master/efcoretest To replicate run the project go to /api/Test then click ‘fetch’ which will create 100 requests for /api/Values.
Let me know if anything needs clarification.