Home > OS >  EF 6 decimal gets dropped when saving to database
EF 6 decimal gets dropped when saving to database

Time:11-29

I'm building a SQL Server / ASP.NET Core MVC application using EF 6 that contains one model with a few decimal properties.

When I enter for example a value of 4.4 in a field and creating the entry, the corresponding property shows as 44,00 in when looking at the overview after that. Another example is a value of 12.1 will become 121,00 in the database.

I'm using the following model. The issue reproduces in all the properties using a decimal type.

public class Driver
{
    public int Id { get; set; }

    [Key]
    
    public string driverName { get; set; }
    public decimal driverScore { get; set; }
    public decimal employedTime { get; set; }
    public int amtApproaches { get; set; }

    [DisplayFormat(NullDisplayText = "No Approaches")]
    public virtual ICollection<Approach>? Approaches { get; set; }
    public int amtEvaluated { get; set; }
    public decimal lastEvaluated { get; set; }

    [DisplayFormat(NullDisplayText = "No Evaluations")]
    public virtual ICollection<Evaluation>? Evaluations { get; set; }
    public bool isPriority { get; set; }

    public Driver()
    {
    }
}

I have used the following code in my DbContext to set the decimal precision if this is of any relevance.

modelBuilder.Entity<Driver>(entity =>
            {
                entity.Property(d => d.driverScore)
                .HasColumnType("decimal(18,1)");
                entity.Property(d => d.employedTime)
                .HasColumnType("decimal(18,1)");
                entity.Property(d => d.lastEvaluated)
                .HasColumnType("decimal(18,1)");
            });

The entered decimal values should be stored correctly (4.4 should remain 4.4) but instead the database drops the decimal and returns a wrong value (44,00 instead of 4.4).

I'm certain this is due to a culture or browser/system settings problem. I'm from The Netherlands where we use comma (,) for decimal separation and dot (.) to indicate thousands. I want to use the US annotation using a decimal point for separation.

CodePudding user response:

I'm certain this is due to a culture or browser/system settings problem. I'm from The Netherlands where we use comma (,) for decimal separation and dot (.) to indicate thousands. I want to use the US annotation using a decimal point for separation.

Yes, as per my investigation upon your code snippet its because of your default-culture. However, we can customize it as per our requirement. You could follow below steps:

Program.cs:

var defaultCulture = "en-US";
var ci = new CultureInfo(defaultCulture);
ci.NumberFormat.NumberDecimalSeparator = "."; // Defining my preferrence for number
ci.NumberFormat.CurrencyDecimalSeparator = ".";

// Configuring Number Seperator Using Localization middleware
app.UseRequestLocalization(new RequestLocalizationOptions
{
    DefaultRequestCulture = new RequestCulture(ci),
    SupportedCultures = new List<CultureInfo>
    {
        ci,
    },
    SupportedUICultures = new List<CultureInfo>
    {
        ci,
    }
});

Note: As you can see in NumberFormat.NumberDecimalSeparator I am setting my preferrence for seperator. You are open to use anything you want, any kind of charaters. Please consider the order as well while placing the code in middleware. Best use would be end of your current middleware. As following:

enter image description here

Output:

enter image description here

Note: For further reference you could check our official document below.

  1. Custom numeric format strings
  2. The "." custom specifier
  • Related