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
Mark
  • 3,717
  • 3
  • 33
  • 48
punkouter
  • 5,170
  • 15
  • 71
  • 116

3 Answers3

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

Mark
  • 3,717
  • 3
  • 33
  • 48
  • There a way I can set this in The Entity Designer? – punkouter Apr 05 '11 at 13:25
  • 1
    @punkouter I edited my posting with an image where you can set the Scale in Entity Framework – Mark Apr 05 '11 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 Apr 05 '11 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.

RB.
  • 36,301
  • 12
  • 91
  • 131
  • 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 Apr 05 '11 at 13:26
-1

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

radu florescu
  • 4,315
  • 10
  • 60
  • 92
misskitty
  • 7
  • 1