Precision and Scale

Discussion of open issues, suggestions and bugs regarding Entity Developer - ORM modeling and code generation tool
Post Reply
strandedpirate
Posts: 21
Joined: Tue 07 Jan 2014 12:38

Precision and Scale

Post by strandedpirate » Wed 17 Sep 2014 12:06

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.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Precision and Scale

Post by Shalex » Thu 18 Sep 2014 13:12

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.

strandedpirate
Posts: 21
Joined: Tue 07 Jan 2014 12:38

Re: Precision and Scale

Post by strandedpirate » Thu 18 Sep 2014 22:03

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 #>)<#+
        }

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Precision and Scale

Post by Shalex » Mon 22 Sep 2014 11:11

Thank you for the detailed description. We will notify you when the issue is fixed in a predefined DbContext template.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Precision and Scale

Post by Shalex » Thu 25 Sep 2014 15:55

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.

Post Reply