简体   繁体   English

实体框架发布后插入时的十进制问题

[英]Entity Framework decimal problem on insert after publish

I have a data model named Gold with a decimal property for the price of gold like this: 我有一个名为Gold的数据模型,它具有一个十进制的黄金价格属性,如下所示:

public class Gold
{
  [key]
  public int GoldId { get; set; }
  public decimal RateValue { get; set; }
}

I know that EF decimal default precision and scale is (18,2) and this is enough for me. 我知道EF十进制默认精度和小数位数为(18,2),这对我来说足够了。 I don't want to change it and I do not have any problem on insert decimal values on localhost. 我不想更改它,在本地主机上插入十进制值也没有任何问题。 But after publishing my website on a host, when I try to insert a decimal like 1210.40 it does not insert or sometimes it inserts 1210.00 (with wrong scale) in db without any error! 但是在主机上发布我的网站后,当我尝试插入1210.40这样的小数时,它不会插入,或者有时它会在db中插入1210.00(比例错误)而没有任何错误!

I completely confused because I don't have this problem on local running of my code. 我完全感到困惑,因为在本地运行代码时没有这个问题。 No problem with decimals like 1210 without the right of dot digits!!! 没有点数字的权利的小数(例如1210)没有问题!!!

This is my code for insert a decimal: 这是我插入小数的代码:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include="RateValue")]Gold manualPrices)
{
  Gold ons = new Gold() {RateValue=(decimal)manualPrices.RateValue,};
            db.GoldRepository.Insert(ons);
            db.Save();
}

Razor Code: 剃刀代码:

@Html.EditorFor(model => model.RateValue, new { htmlAttributes = new { @class = "form-control" } })

more info: I am using MVC .Net Framework 4.5.2 , Code first, Unit of Work, MS SQL 2016. 更多信息:我正在使用MVC .Net Framework 4.5.2,代码优先,工作单元,MS SQL 2016。

Thanks to @StephenMuecke and @heuristican 感谢@StephenMuecke和@heuristican

The culture have been changed to Persian culture in global. 在全球范围内,该文化已更改为波斯文化。 This affects the character used for the decimal separator from dot(.) to slash (/) so decimals with dot as separator don't even bind. 这会影响用于小数点分隔符(从点(。)到斜杠(/)的字符,因此以点为分隔符的小数甚至不会绑定。

Nothing changed in code. 代码没有任何变化。 Just typing decimals like 1210/40 instead of 1210.40 for insert. 只需输入1210/40之类的小数,而不是1210.40进行插入。 Problem solved. 问题解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM