2

I am new to MVC, I use database first (sql server 2008) approach using EF6. What my problem is I am trying to save a decimal value with 3 decimal places, saving is okay but when I view it in my SQL Server 2008 database it saved 2 precise decimal places then followed by zeros.
For example I am trying to save 0.025, I can see it passed 0.025 in my save method but when I view what is saved in my db it is 0.0200, database data type is decimal(18,4). What am I doing wrong? Please help!

UPDATE:
I may have found similar question asked and answered that I have to set the decimal precision in model creating but how can I do this as it seem to be fitted only to code-first approach:
Set decimal(16, 3) for a column in Code First Approach in EF4.3

modelBuilder.Properties<decimal>().Configure(c => c.HasPrecision(18, 3));
Community
  • 1
  • 1
super-user
  • 1,017
  • 4
  • 17
  • 41

2 Answers2

1

The process is not as clear as it could be, but you do it in the properties. See these 2 links:

Entityframework model first decimal precision

With picture:

Why does my Entity Framework turn '2.87' into just '2' (decimal field)?

Community
  • 1
  • 1
Steve Greene
  • 12,029
  • 1
  • 33
  • 54
-1

I see the answer however to long story short, here is the answer

Step 1 Change the data type of the sql server database table to 'float'

Step 2 Change the corresponding ASP.NET model property data type to 'double'

This will solve the problem easily.

However you may get error while saving (in create) and editing (in edit) view. To solve these problems, simply change the @Html.EditFor to manual text box with same name as the property name.

Similarly in the list and details also, use the model property instead of @Html.DisplayFor method.

For those who are still not clear about how to change the view, model data type and database table data type, here is an article explaining this.

However if you know basics of ASP.NET MVC and Entity Framework, no need to read the complete article.

Sheo Narayan
  • 1,196
  • 2
  • 14
  • 17