Home > front end >  Microsoft.EntityFrameworkCore.DbUpdateException: 'An error occurred while saving the entity cha
Microsoft.EntityFrameworkCore.DbUpdateException: 'An error occurred while saving the entity cha

Time:11-19

After upgrading to .net6.0 version while saving 10 digit decimal value - for example - 1234567890.12345, this error occurs:

OverflowException: Conversion overflows

Please suggest how to fix this error.

This error is occurring on the line while saving:

dbcontext.SaveChanges()

In the database, the Value column is defined as DECIMAL(35,17).

Let me know to solve this conversion overflows issue.

CodePudding user response:

By default in EF Core (and previous versions), if you have not specified the precision of a Decimal typed column in the EF model, then it will default to a precision of 18 digits and 2 decimal places, (18,2).

Most likely in EF6 you had a convention to define all decimals as DECIMAL(35,17)

In .Net 6 there is support for conventions again, we now add this to the DbContext:

protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
    // Decimal (37,12) convention
    configurationBuilder.Properties<decimal>()
        .HavePrecision(37, 12);
}

based on solution from Loop/reflect through all properties in all EF Models to set Column Type


EF Core didn't support the same model for configuration conventions as EF6 OOTB, it sucked for a time, but we moved on, here is an old discussion on the topic: Where are Entity Framework Core conventions? also have a read here for

One of the benefits to this is that the model configuration is the standard place for this type of logic, so by keeping the config in-line, or atleast within your DbContext project your conventions are now more discoverable. In EF6 many of us implemented custom conventions and packaged them in distributable libraries for re-use. This effectively made them black-boxes which would often result in local variants of the same conventions so that the current project developers had access to the logic when they needed to inspect it or change it.

  • Related