Why does my Entity Framework turn '2.87' i

2019-01-25 16:37发布

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条回答
太酷不给撩
2楼-- · 2019-01-25 17:24

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.

查看更多
我只想做你的唯一
3楼-- · 2019-01-25 17:25

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

查看更多
女痞
4楼-- · 2019-01-25 17:32

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

查看更多
登录 后发表回答