如果项目存在则创建或更新

时间:2017-07-19 07:49:20

标签: c# asp.net-mvc

我有一个创建表单,如果存在特定的Medicine,其供应数量将更新或添加新条目,但如果特定Medicine不存在,则会创建一批新数据。

我无法理解更新如何在MVC中运行。

这是错误:

存储更新,插入或删除语句会影响意外的行数(0)。自实体加载以来,实体可能已被修改或删除。

这是我的控制器:

public ActionResult Create([Bind(Include = "SupplyID,MedicineID,Expiration,NumberOfSupply")] Supply supply)
    {
        if (ModelState.IsValid)
        {
            bool supplyExsist = db.Supplies.Any(x => x.Expiration == supply.Expiration && x.MedicineID == supply.MedicineID);

            if (supplyExsist)
            {
                var currentSupply = (from x in db.Supplies //get current supply
                                     where x.MedicineID == supply.MedicineID
                                     && x.Expiration == supply.Expiration
                                     select x.NumberOfSupply).First();

                db.Entry(supply).State = EntityState.Modified;
                supply.NumberOfSupply = currentSupply + supply.NumberOfSupply;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            else
            {
                db.Supplies.Add(supply);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
        }

        ViewBag.MedicineID = new SelectList(db.Medicines, "MedicineID", "MedicineName", supply.MedicineID);
        return View(supply);
    }

型号:

public class Supply
{
    [Key]
    public int SupplyID { get; set; }

    [ForeignKey("Medicine")]
    public int MedicineID { get; set; }
    public Medicine Medicine { get; set; }

    [DataType(DataType.Date)]
    public DateTime Expiration { get; set; }

    [Display(Name = "Quantity")]
    [Range(1, int.MaxValue, ErrorMessage = "The value must be greater than 0")]
    public int NumberOfSupply { get; set; }
}

2 个答案:

答案 0 :(得分:0)

试试这个

Option Explicit 'Using this as very first line will ensure all variables are declared.

Public Sub ProductPicture()
    Dim iRow As Long            'row iteration
    Dim lRow As Long            'last used row

    Dim wsSrc As Worksheet      'source worksheet
    Dim wsDest As Worksheet     'destination worksheet

    Set wsSrc = Sheet1          'set source worksheet
    Set wsDest = Sheet2         'set destination worksheet


    lRow = wsSrc.Cells(wsSrc.Rows.Count, 1).End(xlUp).Row   'find last used row

    For iRow = 2 To lRow
        If wsSrc.Cells(iRow, 1) = wsDest.Range("C4").Value Then
            With wsDest.Range("D9:G17")
                .Clear  'If you clear, then always clear BEFORE copy
                        'because `clear` kills the for copy selected range
                        'like `Application.CutCopyMode = False`

                wsSrc.Cells(iRow, 2).Copy    'copy from source

                .PasteSpecial xlPasteAll     'paste into destination range: see `With …`
            End With

            'Exit For
            'probably this is the position you might want the Exit For
            'instead of below
        End If

        Exit For 'exit for at this position doesn't make sense at all,
                 'because it will ALWAYS exit here without iterating iRow
                 'you don't need a loop then if this was your aim.
    Next iRow
End Sub

它会检查db中是否有一个具有相同药物ID的行,如果没有添加新的,则更新它

答案 1 :(得分:0)

您应该使用以下内容更改if块:

if (supplyExsist)
            {
                var currentSupply = (from x in db.Supplies //get current supply
                                     where x.MedicineID == supply.MedicineID
                                     && x.Expiration == supply.Expiration
                                     select x.NumberOfSupply).First();

                db.Supplies.Attach(supply);
                db.Entry(supply).State = EntityState.Modified;
                supply.NumberOfSupply = currentSupply + supply.NumberOfSupply;
                db.SaveChanges();
                return RedirectToAction("Index");
            }