EF:更新条目时发生错误

时间:2015-12-19 11:35:14

标签: c# asp.net entity-framework

我请求帮助。这段代码不起作用

switch (productid)
            {
                case ("Forester"):
                    prdct = db.Products.Find(2);
                    break;
                case ("Impreza"):
                    prdct = db.Products.Find(3);
                    break;
                case ("Legacy"):
                    prdct = db.Products.Find(4);
                    break;
                case ("Tribeca"):
                    prdct = db.Products.Find(5);
                    break;
                case ("XV"):
                    prdct = db.Products.Find(6);
                    break;
                case ("OutBack"):
                    prdct = db.Products.Find(7);
                    break;
            }

cstmr = db.Customers.Find(id);
pchase.CustomerID = cstmr.Id;
pchase.ProductID = prdct.Id;
pchase.PurchaseDate = DateTime.Now;
pchase.PurchaseDate = DateTime.Now;
pchase.ProductsQuantity = 2;
db.Purchases.Add(pchase);
db.SaveChanges();

但是,几乎相同的方法有效

cstmr = db.Customers.Find(Convert.ToInt32(pw.customerid.Text));
prdct = db.Products.Find(Convert.ToInt32(pw.productid.Text));
pchase.CustomerID = cstmr.Id;
pchase.ProductID = prdct.Id;
pchase.PurchaseDate = (DateTime)pw.purchasedate.SelectedDate;
pchase.DeliveryDate = (DateTime)pw.deliverydate.SelectedDate;
pchase.ProductsQuantity = Convert.ToInt32(pw.quantity.SelectedItem);

'id'是客户的ID并正确传递给方法 我试图解决这个问题是徒劳的,我不知道该怎么做。

enter image description here

0 个答案:

没有答案