When using the DbQuery<> ef seems to struggle with no results and projections.
I have a stored procedure on my database which I call like so:
```C#
var results = await _context.QueryType
.FromSql("[dbo].[myQuery] {0}", queryParam)
.Select(qt => new PublicTypeFromQueryType
{
// set properties, configure, etc
})
.ToArrayAsync(cancellationToken)
.ConfigureAwait(false);
return results;
when there are some results everything is groovy. However when there are no results I get the following
Exception message: An exception occurred in the database while iterating the results of a query for context type 'MyQueryReadContext'.
Stack trace:System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.EntityFrameworkCore.Storage.TypedRelationalValueBufferFactoryFactory.CacheKey.<>c.
at System.Linq.Enumerable.AggregateTSource,TAccumulate
at System.Collections.Generic.ObjectEqualityComparer1.GetHashCode(T obj)
at System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey key, Func2 valueFactory)
at Microsoft.EntityFrameworkCore.Internal.NonCapturingLazyInitializer.EnsureInitialized[TParam,TValue](TValue& target, TParam param, Func2 valueFactory)
at Microsoft.EntityFrameworkCore.Query.Internal.ShaperCommandContext.NotifyReaderCreated(DbDataReader dataReader)
at Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable1.AsyncEnumerator.<BufferlessMoveNext>d__12.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.SqlServer.Storage.Internal.SqlServerExecutionStrategy.<ExecuteAsync>d__72.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.Query.Internal.AsyncQueryingEnumerable1.AsyncEnumerator.<MoveNext>d__11.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.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor1.EnumeratorExceptionInterceptor.
I have found a way around this
```C#
var results = await _context.QueryType
.FromSql("[dbo].[myQuery] {0}", queryParam)
.ToArrayAsync(cancellationToken)
.ConfigureAwait(false);
return results
.Select(qt => new PublicTypeFromQueryType
{
// set properties, configure, etc
});
I can understand why ef core would respond in this manner however the error message is not intuitive. Is this an issue that could be addressed?
EF Core version: 2.1
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2017 15.7.6
Note for triage: possibly related #12570 and #12942, but also possibly not.
We're constantly getting these null reference exceptions and have to put a ton of guards in. Entity Framework 6 (and before!) never had these issues and would put in default values for anything that resulted in null on a non-nullable field.
Worse, it doesn't tell you WHAT field is null that's freaking it out.
AND the same happens with where/order bys and doesn't tell you what field is null and freaking out. It should know both and be able to tell you explicitly what field has the issue at the very least. Otherwise it's like finding a neddle in a complex query haystack.
Note that this is especially bad with doing left joins.
This works in 3.1