Ошибка начала происходить все внезапно без изменений в коде или что-либо еще. Может быть добавлены индексы после генерации контекста и сущности.
Это дотнет ядро 2.1.3, а тип таблицы и сущности совпадают.
в сущности это int Year {get; set;}
в БД это Year (int, not null)
Я даже восстановил Контекст и сущности из БД. Но ничего не изменилось.
Я получаю эту ошибку, когда внедрение контекста не связано ни с каким запросом.
Полные ошибки приведены ниже. Я всегда получаю 2 ошибки, которые почти одинаковы.
Exception has occurred: CLR/System.InvalidOperationException
Exception thrown: 'System.InvalidOperationException' in System.Private.CoreLib.dll: 'An exception occurred while reading a database value for property 'AskingPrice.Year'. The expected type was 'System.Nullable`1[System.Int16]' but the actual value was of type 'System.Int32'.'
Inner exceptions found, see $exception in variables window for more details.
Innermost exception System.InvalidCastException : Unable to cast object of type 'System.Int32' to type 'System.Nullable`1[System.Int16]'.
at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityMaterializerSource.TryReadValue[TValue](ValueBuffer valueBuffer, Int32 index, IPropertyBase property)
Exception has occurred: CLR/System.InvalidOperationException
An exception of type 'System.InvalidOperationException' occurred in System.Private.CoreLib.dll but was not handled in user code: 'An exception occurred while reading a database value for property 'AskingPrice.Year'. The expected type was 'System.Nullable`1[System.Int16]' but the actual value was of type 'System.Int32'.'
Inner exceptions found, see $exception in variables window for more details.
Innermost exception System.InvalidCastException : Unable to cast object of type 'System.Int32' to type 'System.Nullable`1[System.Int16]'.
at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityMaterializerSource.TryReadValue[TValue](ValueBuffer valueBuffer, Int32 index, IPropertyBase property)
EDIT:
Только что узнал, что это происходит из-за VS Code. Когда я пытаюсь запустить / отладить его, выдает ошибку выше. Но когда я начал использовать dotnet run
, все было просто отлично, и мы использовали Visual Studio для отладки проекта и работали просто отлично.
Надежда помогает кому-то еще.