简体   繁体   中英

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:

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. 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!

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!!!

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.

Thanks to @StephenMuecke and @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. Problem solved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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