修改后的实体未应用于数据库

时间:2018-06-13 08:31:25

标签: c# entity-framework entity-framework-6 linq-to-entities

我正在尝试根据会员的付款更新贷款余额。 付款顺利,按预期插入,但在更新表LOANBAl期间,没有任何修改,下面是我的代码:

public void UpdateLoanBal(MPAreceipting mpa, string id)
    {
        using (BOSAEntities db = new BOSAEntities())
        {
            General gn = new General();
            gn.GetUser();
            gn.GetServerDate();

            LoanRepayment lr = new LoanRepayment();
            lr.GetMemberDeduction(loanno);
            var lOANBAL = db.LOANBALs.Find(id);
            var lb = new LOANBAL();

            lb.AuditID = gn.sysUser;
            lb.AuditTime = gn.serverDate;
            lb.Balance = Convert.ToDecimal(lr.loanBalance);
            lb.IntrOwed = Convert.ToDecimal(lr.intOwed);
            lb.LastDate = mpa.dateDeposited;


            db.Entry(lOANBAL).State = EntityState.Modified;

            db.SaveChanges();
        }
    }

1 个答案:

答案 0 :(得分:1)

我使用了lOANBAL这是一个实体并更新了它的属性。

 public void UpdateLoanBal(MPAreceipting mpa, string id)
    {
        var db = new BOSAEntities();
        using (var dbContextTransaction = db.Database.BeginTransaction())
        {
            try
            {
                var lOANBAL = db.LOANBALs.Find(loanno);
                General gn = new General();
                gn.GetUser();
                gn.GetServerDate();
                LoanRepayment lr = new LoanRepayment();
                lr.GetMemberDeduction(loanno);
                lOANBAL.LoanNo = loanno;
                lOANBAL.AuditID = gn.sysUser;
                lOANBAL.AuditTime = gn.serverDate;
                lOANBAL.Balance = Convert.ToDecimal(lr.loanBalance);
                lOANBAL.IntrOwed = Convert.ToDecimal(lr.intOwed);
                lOANBAL.LastDate = mpa.dateDeposited;
                lOANBAL.TransactionNo=lr.

                db.Entry(lOANBAL).State = EntityState.Modified;
                db.SaveChanges();
                dbContextTransaction.Commit();
            }
            catch (DbEntityValidationException Exc)
            {
                dbContextTransaction.Rollback();
                string errormessage = string.Join(";",
                    Exc.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage));
                throw new DbEntityValidationException(errormessage);
            }
        }
    }