3
I am making an entry in the database (SQL Server 2008) of a field configured to 'decimal(10,4)', from which I try to insert/edit, by my application C# MVC5, a field of my model of type 'decimal''.
The fact is that when I debug the code, even before the time of 'context.Savechanges()', it has the correct decimal places, e.g.: 0.8999. However, when I do Savechanges it ends up recording in the database as follows '0.8900'.
I am using EF 6.1.3 with reverse engineering to generate the models (Dbfirst). When I enter the command below in my context class that is automatically generated, it works normally, but I have to change it 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 recorded with 4 decimal places, but with only 2.
This 'Decimalmodelbinder' I would implement where? Because if I have to implement in my context class, it will be deleted every update of the classes (via Dbfirst)...
– Antônio Filho
There is no way for me to create an auxiliary 'Onmodelcreating', and only complement this method with some lines of code adding to the 'Onmodelcreating' of the context class?
– Antônio Filho
@Antôniofilho I wear one namespace of the kind:
MeuProjeto.Infrastructure.ModelBinders
. Then you do not run the risk of being deleted. It is up to you. Note that a model Binder no need to accompany the Code First simply because it is part of the application, and not the bank, this conversion.– Leonel Sanches da Silva
Even if this answer was accepted I don’t understand how it helps the AP. AP clearly states that the value is correctly assigned to the property before writing to the database. Therefore the problem cannot be related to bindings
– Bruno Costa
I noticed now that it is already 3 years old and therefore my vote does not even make sense. I am sorry
– Bruno Costa