In tracking query statemanager would throw such exception. For non-tracking we just skip it and return null instead.
I investigated this. Tracking or not, we throw exception early while trying to read value.
Message:
System.Data.SqlTypes.SqlNullValueException : Data is Null. This method or property cannot be called on Null values.
Stack Trace:
SqlBuffer.ThrowIfNull()
SqlBuffer.get_String()
SqlDataReader.GetString(Int32 i)
lambda_method(Closure , DbDataReader , Int32[] )
BufferedDataRecord.ReadObject(DbDataReader reader, Int32 ordinal, ReaderColumn column) line 1297
BufferedDataRecord.ReadRow() line 897
BufferedDataRecord.Initialize(DbDataReader reader, IReadOnlyList`1 columns) line 605
BufferedDataReader.Initialize(IReadOnlyList`1 columns) line 117
RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject) line 423
Enumerator.InitializeReader(DbContext _, Boolean result) line 236
ExecutionStrategy.ExecuteImplementation[TState,TResult](Func`3 operation, Func`3 verifySucceeded, TState state) line 173
ExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded) line 159
Enumerator.MoveNext() line 185
LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
EnumerableHelpers.ToArray[T](IEnumerable`1 source)
Enumerable.ToArray[TSource](IEnumerable`1 source)
FromSqlQueryTestBase`1.Key_entity_with_null_key_value() line 987
We should wrap it in a better exception, at least indicating the column name
Notes from team triage: handle by bringing back EnableDetailedErrors. We should re-investigate the perf and determine whether to switch it on or off by default.
We had existing issue #15751 to bring this back. I am going to use that issue to bring it back. Re-purposed this to profile it to determine default.
Not sure how to vote for an issue, but I +1 this. It would be SUPER helpful to be able to identify which column is causing issues.
@michaelakin - That is already possible by using this API
This issue is about if the exception catching mechanism be default or opt-in by user.
@smitpatel I just tried that and there was no difference in the output of the exception. It still didn't give me the property name that has an issue.
@michaelakin - Then you may not be selecting a property. The exception can tell name of the property only when property is being projected out. Please file a new issue with detailed repro steps and exception details. This discussion deviates from the purpose of this specific issue.
Most helpful comment
I guess it is time we uncomment this stuff?
https://github.com/dotnet/efcore/blob/2972c624cbbae994e6a4936aafdc70c9f960bb1c/src/EFCore.Relational/Query/RelationalShapedQueryCompilingExpressionVisitor.RelationalProjectionBindingRemovingExpressionVisitor.cs#L224-L251