实体框架(5.0)实体未从数据库刷新

时间:2016-04-29 17:43:59

标签: asp.net entity-framework

将记录成功保存到数据库后,本地数据上下文仍会显示更改前的数据。

 public ActionResult Edit(Payment y)
    {
        ViewBag.Message = "Edit DCS LC Payments";
        DCSrecurringInvoicesEntities dcx = new DCSrecurringInvoicesEntities();
        var pvm = dcx.Payment.Where(x => x.PaymentID == y.PaymentID).First();
        pvm.ClientID = y.ClientID;
        pvm.PaymentID = y.PaymentID;

        pvm.DueDate = y.DueDate;
        pvm.MonthlyPayment = y.MonthlyPayment;
        pvm.ReceivedDate = y.ReceivedDate;
        pvm.Amount41098 = y.Amount41098;
        pvm.Paymentmethod = y.Paymentmethod;
        pvm.Penalty = y.Penalty;
        pvm.comment = y.comment;
         pvm.Discount = y.Discount;
        pvm.PaymentTerm = y.PaymentTerm;

        dcx.SaveChanges();
        // dcx.Dispose();  does not refresh data context
        // dcx.Payment.Attach(pvm); does not refresh data context
        return View("Index");
    }

1 个答案:

答案 0 :(得分:0)

我相信只有在调用SaveChanges时添加的实体上才设置主键。