Arithmetic Overflow Error When running Update-Database EFCore

983 views Asked by At

I'm getting this error while running Update-Database in EF Core:

Arithmetic overflow error converting numeric to data type numeric.
The statement has been terminated.

This SQL segment is also highlighted.

Failed executing DbCommand (10ms) [Parameters=[], CommandType='Text', CommandTimeout='30']

IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'DiverId', N'CreatedAt', N'DriverId', N'EmployeeNumber', N'Name', N'SiteId', N'UpdatedAt') AND [object_id] = OBJECT_ID(N'[Drivers]'))
SET IDENTITY_INSERT [Drivers] ON;
INSERT INTO [Drivers] ([DiverId], [CreatedAt], [DriverId], [EmployeeNumber], [Name], [SiteId], [UpdatedAt])
VALUES (1, '2020-04-30T10:41:02.0000000', -9193900000000000000.0, 119642, N'WDE274YE TOCHUKWU', -9141790000000000000.0, '2020-06-01T03:01:34.0000000'),
(2, '2020-04-30T10:41:02.0000000', -4987412556426210000.0, 419079, N'DRIVER ABUBAKAR', -9141790000000000000.0, '2020-06-01T03:01:34.0000000');

IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'DiverId', N'CreatedAt', N'DriverId', N'EmployeeNumber', N'Name', N'SiteId', N'UpdatedAt') AND [object_id] = OBJECT_ID(N'[Drivers]'))
SET IDENTITY_INSERT [Drivers] OFF;

Here is the model class Driver:

public class Driver 
{
        public int Id { get; set; }
        public DateTime CreatedAt { get; set; }
        public DateTime UpdatedAt { get; set; }
        [Column(TypeName = "decimal(18,10)")]
        public decimal SiteId { get; set; }
        [Column(TypeName = "decimal(18,10)")]
        public decimal DriverId { get; set; }
        public string Name { get; set; }
        public int EmployeeNumber { get; set; }
}
1

There are 1 answers

0
Aragorn On

It looks like you are seeding your database with some data. Possible problems:

  • DriverId and SiteId maybe have incorrect data types specified. You’ve set it do decimal(18,10). That means you only have 8 digits available on the left side of the decimal point. Decimal in general seems strange for an Id field. Usually it’s int or bigint.
  • Your seed data may be incorrect. For example you are trying to insert -9193900000000000000.0 as DriverId. It cannot fit in decimal(18,10). This is the largest decimal(18,10) number: 99999999.9999999999 (18 digits in total, but 10 digits reserved for the part after the decimal point).