开发者

Does Entity Framwork round off decimal value to nearest integer?

开发者 https://www.devze.com 2022-12-13 00:39 出处:网络
I\'m using Entity Framework and MS SQL Server 2008. I开发者_如何学Ct seems that Entity Framework always round off decimal entity attribute to nearest integer which is really annoying. Have you seen th

I'm using Entity Framework and MS SQL Server 2008. I开发者_如何学Ct 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!


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


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.


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?


in C#, int / int = int

10 / 2 = 5, 10/3 = 3.

The language with truncate the answer (not round) so if you want to get accurate, you need to cast the variable into doubles or decimals.

0

精彩评论

暂无评论...
验证码 换一张
取 消