Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top