5

I verfied that the correct value '2.87' is coming into the service.. and accord to the EF diagram the type for the 'Score' field is 'Decimal'... But in the database it says just '2'

   [OperationContract]
        public void AddHighScore(string strName, decimal dScore, int iLevel)
        {
            using (SQL2008R2_789485_punkouterEntities1 dc = new SQL2008R2_789485_punkouterEntities1())
            {
                HighScore oHighScore = new HighScore();
                oHighScore.Level = iLevel;
                oHighScore.Name = strName;
                //oHighScore.Name = dScore.ToString();
                oHighScore.Score = dScore;
                dc.AddToHighScores(oHighScore);
                dc.SaveChanges();
            }
        }


-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'HighScores'
CREATE TABLE [dbo].[HighScores] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Score] decimal(18,0)  NOT NULL,
    [Level] int  NOT NULL
);
GO

3 Answers 3

19

You need to set the scale on the decimal field. Change the Score field to be decimal(18,2)

See Decimal help File on MSDN

You can set the Scale in EF by first selecting the field, then in the properties window you will see a property for Scale (see image)

enter image description here

3
  • There a way I can set this in The Entity Designer?
    – punkouter
    Commented Apr 5, 2011 at 13:25
  • 1
    @punkouter I edited my posting with an image where you can set the Scale in Entity Framework
    – Mark
    Commented Apr 5, 2011 at 13:38
  • I found that earlier.. and it said none and I clicked on it and saw nothing.. then I tried (18,2) and that didnt work.. but ok.. i get it now . thanks.
    – punkouter
    Commented Apr 5, 2011 at 18:15
7

decimal(18,0) means a decimal number with 18 digits to the left of the decimal point, and 0 to the right.

Therefore, your value is being stored as 2. I suggest using decimal(18,2) or similar, which allows 18 digits, with up to 2 to the right of the decimal point.

1
  • Ok thanks. Though if I can't set this in the entity designer somehow then Ill have to manually change the genreated sql.. thats a pain.
    – punkouter
    Commented Apr 5, 2011 at 13:26
-1

Change your column datatype to Money (instead of decimal) and regen your edmx.

2
  • -1: there is no such type, at least in C#. And if you meant VisualBasic, please note that it's Money == C#'s decimal. Commented Apr 29, 2012 at 21:11
  • I think @misskitty says about sql money Commented Feb 3, 2020 at 19:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.