Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
469 views
in Technique[技术] by (71.8m points)

sql - Why does my Entity Framework turn '2.87' into just '2' (decimal field)?

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...