C # MVC5 - Insert Fields with 4 Decimal Digits

3

I'm doing an insert into the database (SQL Server 2008) of a field set to 'decimal (10,4)', of which I try to insert / edit, through my C # application MVC5, a field of my model type 'decimal'.

The fact is that when I debug the code, even before the moment of 'context.SaveChanges ()', it has the correct decimal places, eg 0.89999. However, when I do the SaveChanges it ends up writing to the database as follows '0.8900'.

I'm using EF 6.1.3 with reverse engineering to generate the models (DbFirst). When I enter the command below into my context class that is automatically generated, it works normally, however I have to change every time the database is updated.

modelBuilder.Entity<Class>().Property(object => object.property).HasPrecision(12, 10);

NOTE: All my methods that I need to insert fields in this condition are also not written with the four decimal places, but with only 2.     

asked by anonymous 09.07.2015 / 22:08

2 answers

1

Implement a ModelBinder like this:

public class DecimalModelBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        object result = null;

        string modelName = bindingContext.ModelName;
        string attemptedValue = bindingContext.ValueProvider.GetValue(modelName).AttemptedValue;

        string wantedSeperator = NumberFormatInfo.CurrentInfo.NumberDecimalSeparator;
        string alternateSeperator = (wantedSeperator == "," ? "." : ",");

        if (attemptedValue.IndexOf(wantedSeperator) == -1
            && attemptedValue.IndexOf(alternateSeperator) != -1)
        {
            attemptedValue = attemptedValue.Replace(alternateSeperator, wantedSeperator);
        }

        try
        {
            // Faça um Debug desta lista. 
            // Se precisar, amplie a lógica dela para aceitar 4 casas.
            result = decimal.Parse(attemptedValue, NumberStyles.Any);
        }
        catch (FormatException e)
        {
            bindingContext.ModelState.AddModelError(modelName, e);
        }

        return result;
    }
}

Do not forget to register ModelBinder in Global.asax.cs :

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        ...

        ModelBinders.Binders.Add(typeof(decimal), new DecimalModelBinder());
        ModelBinders.Binders.Add(typeof(decimal?), new DecimalModelBinder());
        ModelBinders.Binders.DefaultBinder = new CustomModelBinder();

        ...
     }
}
    
10.07.2015 / 00:11
0
  

When I enter the command below into my context class which is   generated automatically, it works normally, but I have to change   every time the database is updated.

You should not modify your context code once it is generated automatically. If you need to make changes to your context, make a separate file. Notice that the context is generated with the keyword partial . This allows you to implement the logic in a different file that is never modified by the automatic code generator.

partial public OMeuContexto{

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Class>().Property(o => o.property).HasPrecision(12, 10);
    }
}

Once again I draw your attention. Do this in another file that is not automatically generated

    
02.03.2018 / 00:00