views:

159

answers:

3

Hi all,

I'm using Entity Framework and MS SQL Server 2008. It seems that Entity Framework always round off decimal entity attribute to nearest integer which is really annoying. Have you seen this problem before and what's your solution? Thanks!

A: 

Without you providing your mapping classes and database schema I can only assume that in one of them you're using an Int or something that's set to zero decimal places of accuracy.

It would also be worth mentioning which version of EF (Entity Framework) you're using.

Timothy Walters
A: 

It shouldn't happen is the short answer.

Similar to what Timothy said, it's likely that one of the following is happening:

  • A property of one of your generated classes is defined as an int while the column is a decimal - you can do this by changing the actual type in the entity designer
  • Somewhere in your code, you're working with ints rather than decimals. In particular, if you're using implicit typing using var, the compiler could be inferring an integral type rather than a decimal type

An example to clarify the second point:

var myNumber = 100;    // myNumber will be an int

myNumber = myNumber / 3;    // myNumber == 33 (an int)

Can you post some code to give us a better idea?

Damovisa
+1  A: 

hi

i run into the same problem. I am using EF with SQL Server 2008 SP1. I use stored procedures to query some data. Another annoying thing is that if I create a function import for a stored procedure, EF supplies a function on the context class to call that sp, but for me, it does not. So I create my own function to call sp. In that functions I create EntityParameters by supplying DBType.Decimal. But this result in Precision and Scale set to 0. When I set to the correct numbers as my DB schema requires (3 and 1 btw), everything is ok.

zsolt

related questions