System.InvalidOperationException: 'An exception occurred while reading a database value. The expected type was 'System.Int32' but the actual value was of type 'System.Int16'.'
See original GitHub issueThe following Linq query generates the titled exception.
from e in es
join d in ds on e.Id equals d.EmployeeId into x
from j in x.DefaultIfEmpty()
select new Holder
{
Name = e.Name,
DeviceId = j.DeviceId //(short?)
}
es and ds are DbSets. DeviceId is type of int?. j.DeviceId is type of short.
If we remove DefaultIfEmpty(), then we don’t get the exception.
Please refer to the Steps to reproduce for complete source code.
Exception message:
System.InvalidOperationException: 'An exception occurred while reading a database value. The expected type was 'System.Int32' but the actual value was of type 'System.Int16'.'
Stack trace:
at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityMaterializerSource.ThrowReadValueException[TValue](Exception exception, Object value, IPropertyBase property)
at Microsoft.EntityFrameworkCore.Storage.Internal.TypedRelationalValueBufferFactory.Create(DbDataReader dataReader)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.BufferlessMoveNext(DbContext _, Boolean buffer)
at Microsoft.EntityFrameworkCore.Storage.Internal.NoopExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.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 TestHostForCastException.Program.Main(String[] args) in C:\EFCoreTestBed\Program.cs:line 23
Inner Exception 1:
InvalidCastException: Specified cast is not valid.
Steps to reproduce
Repro included at https://github.com/avinash-phaniraj-readify/EFCoreTestBed/tree/CastErrorInProjection
Further technical details
EF Core version: 2.1 Database Provider: EntityFrameworkCore.SqlServerCompact35 Operating system: Windows 10.0.17134 IDE: Visual Studio 2017 15.7.5
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
LINQ query returns error "The expected type was 'System. ...
InvalidOperationException : An exception occurred while reading a database value for property 'DepartmentSharing.ContentId'. The expected type ...
Read more >'An exception occurred while reading a database value. ...
InvalidOperationException : 'An exception occurred while reading a database value. The expected type was 'System.Int32' but the actual value ...
Read more >System.InvalidOperationException: 'An exception occurred ...
System.InvalidOperationException: 'An exception occurred while reading a database value. The expected type was 'System.Int32' but the actual ...
Read more >Using NUMBER(5) in db with EF Core 2.2
InvalidOperationException : An exception occurred while reading a database value for property 'Table.ColumnX'. The expected type was 'System.
Read more >System.InvalidCastException: Unable to cast object of type ...
HI,. I received this message : System.InvalidCastException: Unable to cast object of type 'System.Int64' to type 'System.Int32'.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Confirming that this is now fixed in 2.1.4. Many thanks team.
Re-opening to consider for 2.1.5 patch.