efcore
efcore copied to clipboard
Conversion overflow exception on insert or update a decimal value with DECIMAL(38,10)
File a bug
I get an
OverflowException: Conversion overflows.
on inserting some valid decimal numbers with EF Core. You can try with following numbers:
- 99998888889688880000.00000000
- 39998519999997200103600.0000000000
I can insert and read both numbers with plain T-SQL into the database. I can read those numbers with EF Core, but I cannot insert or update those numbers with EF Core into the database.
You can find a full working repro below. It also shows that those number can be put into the database (by using ExecuteSqlRawAsync)
Include your code
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
await using var ctx = new BlogContext();
await ctx.Database.EnsureDeletedAsync();
await ctx.Database.EnsureCreatedAsync();
await ctx.Database.ExecuteSqlRawAsync(
@"
DECLARE @MyValue1 DECIMAL(38,10) = 99998888889688880000.00000000
DECLARE @MyValue2 DECIMAL(38,10) = 39998519999997200103600.0000000000
INSERT INTO [Blogs]([MyNumber])
VALUES(@MyValue1)
INSERT INTO [Blogs]([MyNumber])
VALUES(@MyValue2)
"
);
foreach (var blog in ctx.Blogs)
{
Console.WriteLine($"BlogId: {blog.Id}, MyValue: {blog.MyNumber}");
}
ctx.Blogs.Add(new Blog());
await ctx.SaveChangesAsync();
public class BlogContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) =>
optionsBuilder
.UseSqlServer(
"Server=localhost;Database=test;User=SA;Password=Abcd5678;Connect Timeout=60;ConnectRetryCount=0;Encrypt=false"
)
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();
}
public class Blog
{
public int Id { get; set; }
[Precision(38, 10)]
public decimal MyNumber { get; set; } = 99998888889688880000.00000000m;
}
Include stack traces
Microsoft.EntityFrameworkCore.DbUpdateException
HResult=0x80131500
Message=An error occurred while saving the entity changes. See the inner exception for details.
Source=Microsoft.EntityFrameworkCore.Relational
StackTrace:
at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.<ExecuteAsync>d__50.MoveNext()
at Microsoft.EntityFrameworkCore.SqlServer.Update.Internal.SqlServerModificationCommandBatch.<ExecuteAsync>d__15.MoveNext()
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.<ExecuteAsync>d__9.MoveNext()
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.<ExecuteAsync>d__9.MoveNext()
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.<ExecuteAsync>d__9.MoveNext()
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.<SaveChangesAsync>d__111.MoveNext()
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.<SaveChangesAsync>d__115.MoveNext()
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.<ExecuteAsync>d__7`2.MoveNext()
at Microsoft.EntityFrameworkCore.DbContext.<SaveChangesAsync>d__63.MoveNext()
at Microsoft.EntityFrameworkCore.DbContext.<SaveChangesAsync>d__63.MoveNext()
at Program.<<Main>$>d__0.MoveNext() in C:\Code\GITHUB-MY\EfCoreBugs\ConsoleApp1\Program.cs:line 25
at Program.<<Main>$>d__0.MoveNext() in C:\Code\GITHUB-MY\EfCoreBugs\ConsoleApp1\Program.cs:line 25
This exception was originally thrown at this call stack:
[External Code]
Inner Exception 1:
OverflowException: Conversion overflows.
Include provider and version information
EF Core version: 8.0.2 Database provider: (Microsoft.EntityFrameworkCore.SqlServer) Target framework: (.NET 8.0) Operating system: Windows IDE: (Visual Studio 2022 17.8.7)