Page 1 of 1

Precision and Scale

Posted: Wed 17 Sep 2014 12:06
by strandedpirate
5.7.441 build with the DBContext template and potentially other templates as well.

Precision and scale are not explicitly set for decimal types in the model configuration. Decimal types default to (18, 2) when not explicitly specified which causes values to be rounded down.

Re: Precision and Scale

Posted: Thu 18 Sep 2014 13:12
by Shalex
Please give us the following information:
1) you are using the Database-First approach (generating model basing on a predefined database), aren't you?
2) have you tried to customize the Database-First mapping via Visual Studio > Tools > Entity Developer > Options > Server Options > SQL Server? Be aware that the new mapping is applied only for the newly created models (or if you will recreate the entities in CSDL basing on SSDL in an existing model). If this doesn't help, please specify the exact steps we should follow in our environment to reproduce the issue.

Re: Precision and Scale

Posted: Thu 18 Sep 2014 22:03
by strandedpirate
1) Database-first - yes
2) Customized options - no

To reproduce:
1) Create a table with a column of type decimal - any precision and any scale you wish.
2) Create a new model in Entity Developer based on this new table with the DbContext template.
3) Generate the code.

Result: There is no explicit call to .HasPrecision() for any columns of type decimal. The Entity Framework defaults decimal types to (18, 2) when HasPrecision is not specified which obviously causes data to be rounded and lossed during insertion if your column's precision/scale are defined differently.

This:

Code: Select all

modelBuilder.Entity<Order>()
                .Property(p => p.SubtotalIncludingTax)
                    .IsRequired()
                    .HasColumnType("decimal");
Should be this:

Code: Select all

modelBuilder.Entity<Order>()
                .Property(p => p.SubtotalIncludingTax)
                    .IsRequired()
                    .HasColumnType("decimal")
                    .HasPrecision(18, 4);
Here is the template code that needs to be added:

Code: Select all

        if (propertyDataType == EntityDataType.Decimal) {
#>

                    .HasPrecision(<#= prop.Precision #>, <#= prop.Scale #>)<#+
        }

Re: Precision and Scale

Posted: Mon 22 Sep 2014 11:11
by Shalex
Thank you for the detailed description. We will notify you when the issue is fixed in a predefined DbContext template.

Re: Precision and Scale

Posted: Thu 25 Sep 2014 15:55
by Shalex
The bug with generating .HasPrecision for decimal types when using non-attribute fluent mapping (DbContext template) is fixed in the new (5.7.451) build of Entity Developer.
It can be downloaded from http://www.devart.com/entitydeveloper/download.html (trial version) or from Registered Users' Area (for users with active subscription only).
For more information, please refer to http://forums.devart.com/viewtopic.php?f=32&t=30472.