2

I have a database field defined as decimal(9,9). The edmx model in C# maps this field to a decimal variable with a precision and scale of 9.

I put the number 5 in the variable. Everywhere I can check shows the value as 5 (in local variables, the entity class instance, data context, etc.).

At the time of DataContex.SaveChanges() the value is converted to "5.000000000". As best I can tell the entity framework is adding 9 decimal places to the value before trying to save to the database. This causes an error because the value now has too many digits.

Does anyone know what causes this behavior and how I can change it?

Addition Information - I started by creating my database in Microsoft SQL 2008 R2. I then generated the Entity Framework model from the database.

Jeremy Lund
  • 107
  • 1
  • 6

2 Answers2

2

If you really have defined your data type as decimal(9,9) in your database, you've got a problem.
According to Microsoft, the first number is the total number of digits, the second the number of digits to the right of the decimal point.
So you can really only store numbers below 1 in such a field, in the format .123456789. If you need to store 5, you will need a different format.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
  • The referenced page list the values as "maximum" values and I read it as the possible limit, not the required ammount. I changed it to 'decimal(9,4)' and everything worked. I'm marking your reply as the asnwer. Thank you. – Jeremy Lund Apr 16 '12 at 18:34
  • Yes, the reference page is ambiguous when you read it like that, but in SQL, decimal is really a fixed-point type (as opposed to C#'s decimal, which is more flexible). – Mr Lister Apr 17 '12 at 05:54
1

This isn't particular to the Entity Framework but, rather, expected for a decimal with a precision defined of 9 (which maps back to your database). So, when you enter 5, because of the way the data is defined, then the 0's will be added after the decimal point.

This was put into place in .NET 1.1 to conform with a standard (I forget which one). For display purposes, you could always use Math.Round to remove a level of precision (or define your data differently for what you actually require).

JasCav
  • 34,458
  • 20
  • 113
  • 170
  • The precision level is already rounded in C#. The extra digits (to the right of the decimal point) are added at the time of save. There is nothing I can remove in C# because the value shows as only 5 (without a decimal point or extra digits). – Jeremy Lund Apr 16 '12 at 17:06
  • @JeremyLund - You can override the precision via the information found in this link here: http://stackoverflow.com/questions/3504660/entity-framework-code-first-decimal-precision. I don't know the answer to your comment directly as I have never run into this specific situation. – JasCav Apr 16 '12 at 17:09
  • Thank you for the suggestion, but I'm using Model First and the Model already contains the correct properties for the precision and scale (9,9) matching the database. That is why I'm so baffeled that it is going beyond this defined precision and scale. – Jeremy Lund Apr 16 '12 at 17:12
  • For display, it would be better perhaps to use a format string rather than Math.Round – phoog Apr 16 '12 at 17:34