Efcore: Add test for...ThenInclude value cannot be null

Created on 15 Jun 2019  路  6Comments  路  Source: dotnet/efcore

var test = await dbcontext.SomeEntity
.Include(x => x.SecondEntity).ThenInclude(x => x.ThirdEntity)
.SingleOrDefaultAsync(x => x.Id == id);

Fails when SecondEntity is null.

Works when ThenInclude is removed. Also works if SecondEntity is not null even if ThirdEntity is null.

Exception message:
Value cannot be null.\r\nParameter name: key

Stack trace:
"   at System.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)\r\n   at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)\r\n   at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)\r\n   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityReferenceMap.TryGet(Object entity, IEntityType entityType, InternalEntityEntry& entry, Boolean throwOnNonUniqueness)\r\n   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.TryGetEntry(Object entity, Boolean throwOnNonUniqueness)\r\n   at Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.RelationalShapedQueryCompilingExpressionVisitor.IncludeCompilingExpressionVisitor.IncludeReference[TEntity,TIncludedEntity](QueryContext queryContext, DbDataReader dbDataReader, TEntity entity, Func`4 innerShaper, INavigation navigation, INavigation inverseNavigation, Action`2 fixup, Boolean trackingQuery, ResultCoordinator resultCoordinator)\r\n   at Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.RelationalShapedQueryCompilingExpressionVisitor.IncludeCompilingExpressionVisitor.IncludeCollection[TEntity,TIncludedEntity](QueryContext queryContext, DbDataReader dbDataReader, TEntity entity, Func`3 outerKeySelector, Func`3 innerKeySelector, Func`4 innerShaper, INavigation navigation, INavigation inverseNavigation, Action`2 fixup, Boolean trackingQuery, ResultCoordinator resultCoordinator)\r\n   at Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.RelationalShapedQueryCompilingExpressionVisitor.AsyncQueryingEnumerable`1.AsyncEnumerator.<MoveNextAsync>d__17.MoveNext()\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.ValueTaskAwaiter`1.GetResult()\r\n   at Microsoft.EntityFrameworkCore.Query.Pipeline.ShapedQueryCompilingExpressionVisitor.<SingleOrDefaultAsync>d__12`1.MoveNext()\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at Microsoft.EntityFrameworkCore.Query.Pipeline.ShapedQueryCompilingExpressionVisitor.<SingleOrDefaultAsync>d__12`1.MoveNext()\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n   at Web.Controllers.DataController.<GetItem>d__8.MoveNext() in xxxxxxxxxxx.cs:line 165"

Steps to reproduce

Have private repo if required

Further technical details

EF Core version: 3.0.0-preview7.19314.2
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2019 16.2.0 Preview 2

closed-fixed customer-reported type-bug

All 6 comments

@kccsf - Thanks for reporting. I believe I know where the bug is. Repro is not necessary. :)

No problem; thanks again for such speedy feedback! :-)

@smitpatel to write a note on how to fix this.

Thanks - working in 3.0.0-preview7.19365.7

@kccsf - Thanks for verifying.
@ajcvickers - Should this moved to backlog to add regression test?

Was this page helpful?
0 / 5 - 0 ratings