question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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 issue

The 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:closed
  • Created 5 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
optikscommented, Sep 20, 2018

Confirming that this is now fixed in 2.1.4. Many thanks team.

1reaction
ajcvickerscommented, Aug 17, 2018

Re-opening to consider for 2.1.5 patch.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found