正如标题所述,我最近遇到了这个问题,对此我感到困惑。该错误如何发生,为什么?我已经正确编写了代码,似乎没有问题。在研究了此类错误之后,我仍然不明白。
public ActionResult AddOrder(OrderDetail orderDetail)
{
string message = "";
if (ModelState.IsValid)
{
using (myDatabaseEntities1 myDatabase1 = new myDatabaseEntities1())
{
String userid = Convert.ToString(myDatabase1.OrderDetails.Where(a => a.User_ID == orderDetail.User_ID).FirstOrDefault());
userid = User.Identity.GetUserId();
DateTime CurrentDT = DateTime.UtcNow;
var Date = Convert.ToDateTime(myDatabase1.OrderDetails.Where(a => a.OrderDate == orderDetail.OrderDate).FirstOrDefault());
Date = CurrentDT;
Double T_Price = Convert.ToDouble(myDatabase1.OrderDetails.Where(a => a.OrderPrice == orderDetail.OrderPrice).FirstOrDefault());
//WF
var A_ChickenChop_BP = myDatabase1.OrderDetails.Where(a => a.A_ChickenChop_BP == orderDetail.A_ChickenChop_BP).FirstOrDefault();
var A_ChickenChop_M = myDatabase1.OrderDetails.Where(a => a.A_ChickenChop_M == orderDetail.A_ChickenChop_M).FirstOrDefault();
var A_Spaghetti_AH = myDatabase1.OrderDetails.Where(a => a.A_Spaghetti_AH == orderDetail.A_Spaghetti_AH).FirstOrDefault();
var A_Spaghetti_P = myDatabase1.OrderDetails.Where(a => a.A_Spaghetti_P == orderDetail.A_Spaghetti_P).FirstOrDefault();
var A_Spaghetti_S = myDatabase1.OrderDetails.Where(a => a.A_Spaghetti_S == orderDetail.A_Spaghetti_S).FirstOrDefault();
//CF
var A_ChickenRice_CB = myDatabase1.OrderDetails.Where(a => a.A_ChickenRice_CB == orderDetail.A_ChickenRice_CB).FirstOrDefault();
var A_ChickenRice_CW = myDatabase1.OrderDetails.Where(a => a.A_ChickenRice_CW == orderDetail.A_ChickenRice_CW).FirstOrDefault();
var A_ChickenRice_D = myDatabase1.OrderDetails.Where(a => a.A_ChickenRice_D == orderDetail.A_ChickenRice_D).FirstOrDefault();
var A_WantanMee_NS = myDatabase1.OrderDetails.Where(a => a.A_WantanMee_NS == orderDetail.A_WantanMee_NS).FirstOrDefault();
var A_WantanMee_IS = myDatabase1.OrderDetails.Where(a => a.A_WantanMee_IS == orderDetail.A_WantanMee_IS).FirstOrDefault();
//WF
Double PriceOfF1 = Convert.ToDouble(A_ChickenChop_BP) * 14.9;
Double PriceOfF2 = Convert.ToDouble(A_ChickenChop_M) * 14.9;
Double PriceOfF3 = Convert.ToDouble(A_Spaghetti_AH) * 10.9;
Double PriceOfF4 = Convert.ToDouble(A_Spaghetti_P) * 10.9;
Double PriceOfF5 = Convert.ToDouble(A_Spaghetti_S) * 10.9;
//CF
Double PriceOfF6 = Convert.ToDouble(A_ChickenRice_CB) * 6.9;
Double PriceOfF7 = Convert.ToDouble(A_ChickenRice_CW) * 6.9;
Double PriceOfF8 = Convert.ToDouble(A_ChickenRice_D) * 6.9;
Double PriceOfF9 = Convert.ToDouble(A_WantanMee_NS) * 6.9;
Double PriceOfF10 = Convert.ToDouble(A_WantanMee_IS) * 6.9;
T_Price = PriceOfF1 + PriceOfF2 + PriceOfF3 + PriceOfF4 + PriceOfF5
+ PriceOfF6 + PriceOfF7 + PriceOfF8 + PriceOfF9 + PriceOfF10;
if (T_Price > 1)
{
myDatabase1.OrderDetails.Add(orderDetail);
myDatabase1.SaveChanges();
message = "The order has been placed successfully";
}
else
{
message = "Please order any food at least one";
}
}
}
ViewBag.Message = message;
RedirectToAction("Index", "Home");
return View(orderDetail);
}
该错误发生在使数据库数据加倍的那一行。如您所见,我做的很多数据都是双倍的,所有这些都解决了这个问题。 顺便说一下,我使数据库中的所有这些数据都是int类型。
[Display(Name = "Order ID")]
public int OrderID { get; set; }
[Display(Name = "User ID")]
public int User_ID { get; set; }
[Display(Name = "Order Date")]
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public Nullable<System.DateTime> OrderDate { get; set; }
[Display(Name = "Order Address")]
[Required(AllowEmptyStrings = false, ErrorMessage = "Address is required for the order")]
public string OrderAddress { get; set; }
[Range(0, int.MaxValue, ErrorMessage = "Amount of the food is not valid")]
[Display(Name = "Amount Of ChickenChop in Black Pepper Sauce")]
public int A_ChickenChop_BP { get; set; }
[Range(0, int.MaxValue, ErrorMessage = "Amount of the food is not valid")]
[Display(Name = "Amount Of ChickenChop in Mushroom Sauce")]
public int A_ChickenChop_M { get; set; }
[Range(0, int.MaxValue, ErrorMessage = "Amount of the food is not valid")]
[Display(Name = "Amount Of Angel Hair Spaghetti")]
public int A_Spaghetti_AH { get; set; }
[Range(0, int.MaxValue, ErrorMessage = "Amount of the food is not valid")]
[Display(Name = "Amount Of Penne Spaghetti")]
public int A_Spaghetti_P { get; set; }
[Range(0, int.MaxValue, ErrorMessage = "Amount of the food is not valid")]
[Display(Name = "Amount Of Shell Spaghetti")]
public int A_Spaghetti_S { get; set; }
[Range(0, int.MaxValue, ErrorMessage = "Amount of the food is not valid")]
[Display(Name = "Amount Of ChickenRice with Chicken Breast part")]
public int A_ChickenRice_CB { get; set; }
[Range(0, int.MaxValue, ErrorMessage = "Amount of the food is not valid")]
[Display(Name = "Amount Of ChickenRice with Chicken Wing part")]
public int A_ChickenRice_CW { get; set; }
[Range(0, int.MaxValue, ErrorMessage = "Amount of the food is not valid")]
[Display(Name = "Amount Of ChickenRice with Drumstick part")]
public int A_ChickenRice_D { get; set; }
[Range(0, int.MaxValue, ErrorMessage = "Amount of the food is not valid")]
[Display(Name = "Amount Of Non-Spicy Wantan Mee")]
public int A_WantanMee_NS { get; set; }
[Range(0, int.MaxValue, ErrorMessage = "Amount of the food is not valid")]
[Display(Name = "Amount Of Spicy Wantan Mee")]
public int A_WantanMee_IS { get; set; }
[Display(Name = "Total Price")]
public Nullable<int> OrderPrice { get; set; }
[Display(Name = "Is the order paid?")]
public Nullable<bool> Is_Paid { get; set; }
有人可以向我解释此错误的发生方式和解决方法吗?
遇到此错误的确切行是
Double T_Price = Convert.ToDouble(myDatabase1.OrderDetails.Where(a => a.OrderPrice == orderDetail.OrderPrice).FirstOrDefault());
//WF
var A_ChickenChop_BP = myDatabase1.OrderDetails.Where(a => a.A_ChickenChop_BP == orderDetail.A_ChickenChop_BP).FirstOrDefault();
var A_ChickenChop_M = myDatabase1.OrderDetails.Where(a => a.A_ChickenChop_M == orderDetail.A_ChickenChop_M).FirstOrDefault();
var A_Spaghetti_AH = myDatabase1.OrderDetails.Where(a => a.A_Spaghetti_AH == orderDetail.A_Spaghetti_AH).FirstOrDefault();
var A_Spaghetti_P = myDatabase1.OrderDetails.Where(a => a.A_Spaghetti_P == orderDetail.A_Spaghetti_P).FirstOrDefault();
var A_Spaghetti_S = myDatabase1.OrderDetails.Where(a => a.A_Spaghetti_S == orderDetail.A_Spaghetti_S).FirstOrDefault();
//CF
var A_ChickenRice_CB = myDatabase1.OrderDetails.Where(a => a.A_ChickenRice_CB == orderDetail.A_ChickenRice_CB).FirstOrDefault();
var A_ChickenRice_CW = myDatabase1.OrderDetails.Where(a => a.A_ChickenRice_CW == orderDetail.A_ChickenRice_CW).FirstOrDefault();
var A_ChickenRice_D = myDatabase1.OrderDetails.Where(a => a.A_ChickenRice_D == orderDetail.A_ChickenRice_D).FirstOrDefault();
var A_WantanMee_NS = myDatabase1.OrderDetails.Where(a => a.A_WantanMee_NS == orderDetail.A_WantanMee_NS).FirstOrDefault();
var A_WantanMee_IS = myDatabase1.OrderDetails.Where(a => a.A_WantanMee_IS == orderDetail.A_WantanMee_IS).FirstOrDefault();
//WF
Double PriceOfF1 = Convert.ToDouble(A_ChickenChop_BP) * 14.9;
Double PriceOfF2 = Convert.ToDouble(A_ChickenChop_M) * 14.9;
Double PriceOfF3 = Convert.ToDouble(A_Spaghetti_AH) * 10.9;
Double PriceOfF4 = Convert.ToDouble(A_Spaghetti_P) * 10.9;
Double PriceOfF5 = Convert.ToDouble(A_Spaghetti_S) * 10.9;
//CF
Double PriceOfF6 = Convert.ToDouble(A_ChickenRice_CB) * 6.9;
Double PriceOfF7 = Convert.ToDouble(A_ChickenRice_CW) * 6.9;
Double PriceOfF8 = Convert.ToDouble(A_ChickenRice_D) * 6.9;
Double PriceOfF9 = Convert.ToDouble(A_WantanMee_NS) * 6.9;
Double PriceOfF10 = Convert.ToDouble(A_WantanMee_IS) * 6.9;