为什么我的实体框架变成了“2.87”变成只是“2” (小数域)?
我验证了正确的值“2.87”正在进入服务..并且根据 EF 图,“分数”字段的类型是“十进制”...但在数据库中它只显示“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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要在小数字段上设置比例。将“分数”字段更改为小数(18,2)
请参阅 MSDN 上的小数帮助文件< /a>
您可以通过首先选择字段来设置 EF 中的比例,然后在属性窗口中您将看到比例的属性(见图)
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)
decimal(18,0)
表示小数点左边 18 位、右边 0 的十进制数。因此,您的值将存储为
2
。我建议使用decimal(18,2)
或类似的,它允许 18 位数字,小数点右边最多 2 位。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 usingdecimal(18,2)
or similar, which allows 18 digits, with up to 2 to the right of the decimal point.将列数据类型更改为 金钱(而不是小数) 并重新生成您的 edmx。
Change your column datatype to Money (instead of decimal) and regen your edmx.