我验证了正确的值'2.87'进入了服务..并根据EF图,'Score'字段的类型为'Decimal'...但是在数据库中它说的只是'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

最佳答案

您需要在十进制字段上设置比例。将分数字段更改为十进制(18,2)

See Decimal help File on MSDN

您可以通过首先选择字段来在EF中设置缩放,然后在属性窗口中将看到Scale的属性(参见图片)

10-06 16:06