I had .NET 2.2 application with EF Core 2.2.6 that was recently upgraded to use NET 5 with EF Core 5.
in MyDBContext
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Order>(entity =>
{
entity.Property(e => e.Confidence).HasColumnType("decimal(18, 0)");
}
}
Test
public async Task Test()
{
var dbContext = new MyDbContext()
var wo = await dbContext.Orders
.Where(x => x.OrderID == 1234)
.SingleOrDefaultAsync();
decimal c = 100M / 3; //ie 33.333333333333333333333333333
wo.Confidence = c;
await dbContext.SaveChangesAsync();
}
With EF Core 2.2.6 it used work. The value was getting passed to SQL server which was truncating the value to 33
However with EF Core 5.0.13, EF throws error before generating SQL.
Microsoft.EntityFrameworkCore.DbUpdateException : An error occurred while updating the entries. See the inner exception for details.
---- System.ArgumentException : Parameter value '33.333333333333333333333333333' is out of range.
I understand the error is because total length is >18. If I hard code the value to 33.3333333333333333M
then it works in EF 5 as well.
I am trying to understand why EF Core 5 does not allow? Is EF Core 5 does implicit validation before generating SQL?