Asp.Net Core:更新行会导致插入

时间:2019-09-11 23:26:01

标签: c# entity-framework-core

我正在尝试更新网格数据,但是它正在插入数据。我在做什么错了?

                foreach (ExistingPositionGridViewModel existingPositionFromUI in gridObject)
                {
                    ExistingPosition existingPosition = new ExistingPosition();
                    existingPosition.Id = existingPositionFromUI.Id;
                    existingPosition.FundAgencyId = existingPositionFromUI.FundAgencyId;
                    existingPosition.NoOfPositions = existingPositionFromUI.NoOfPositions;
                    existingPosition.RegularSalary = existingPositionFromUI.RegularSalary;
                    existingPosition.VacFac = existingPositionFromUI.VacFac;
                    existingPosition.Mappip = existingPositionFromUI.Mappip;
                    existingPosition.Other0111 = existingPositionFromUI.Other0111;
                    existingPosition.Retire = existingPositionFromUI.Retire;
                    existingPosition.DefComp = existingPositionFromUI.DefComp;
                    existingPosition.Retirement162 = existingPositionFromUI.Retirement162;
                    existingPosition.RetireeMed = existingPositionFromUI.RetireeMed;
                    existingPosition.HealthReimb = existingPositionFromUI.HealthReimb;
                    existingPosition.PensionPrepmtDisc = existingPositionFromUI.PensionPrepmtDisc;
                    existingPosition.Unemployment = existingPositionFromUI.Unemployment;
                    existingPosition.SalCont = existingPositionFromUI.SalCont;
                    existingPosition.Health = existingPositionFromUI.Health;
                    existingPosition.WellnessProgram = existingPositionFromUI.WellnessProgram;
                    existingPosition.Dental = existingPositionFromUI.Dental;
                    existingPosition.Life = existingPositionFromUI.Life;
                    existingPosition.AdnD = existingPositionFromUI.AdnD;
                    existingPosition.Other0319 = existingPositionFromUI.Other0319;
                    existingPosition.Medicare = existingPositionFromUI.Medicare;

                    existingPosition.TotalSnEb = ZeroIfNull(existingPositionFromUI.RegularSalary) + ZeroIfNull(existingPositionFromUI.VacFac) + ZeroIfNull(existingPositionFromUI.Mappip) + ZeroIfNull(existingPositionFromUI.Other0111) + ZeroIfNull(existingPositionFromUI.Retire) + ZeroIfNull(existingPositionFromUI.DefComp) + ZeroIfNull(existingPositionFromUI.Retirement162) + ZeroIfNull(existingPositionFromUI.RetireeMed) + ZeroIfNull(existingPositionFromUI.HealthReimb) + ZeroIfNull(existingPositionFromUI.PensionPrepmtDisc) + ZeroIfNull(existingPositionFromUI.Unemployment) + ZeroIfNull(existingPositionFromUI.SalCont) + ZeroIfNull(existingPositionFromUI.Health) + ZeroIfNull(existingPositionFromUI.WellnessProgram) + ZeroIfNull(existingPositionFromUI.Dental) + ZeroIfNull(existingPositionFromUI.Life +
 existingPositionFromUI.AdnD) + ZeroIfNull(existingPositionFromUI.Other0319) + ZeroIfNull(existingPositionFromUI.Medicare);
                    existingPosition.ExecCar = existingPositionFromUI.ExecCar;
                    existingPosition.Obp = existingPositionFromUI.Obp;
                    existingPosition.GrandTotal = ZeroIfNull(existingPosition.TotalSnEb) + ZeroIfNull(existingPositionFromUI.ExecCar) + ZeroIfNull(existingPositionFromUI.Obp);
                    existingPosition.FiscalYearId = _sessionUser.CurrentFiscalYearId;
                    existingPosition.DepartmentId = _sessionUser.DepartmentId;
                    existingPosition.BudgetRequestId = budgetRequest.Id;// existingPositionFromUI.BudgetRequestId;
                    existingPosition.ModifiedBy = _sessionUser.Username;
                    existingPosition.ModifiedDate = DateTime.Now;
                    //existingPosition.CreatedDate = DateTime.Now;
                    //existingPosition.CreatedBy = _sessionUser.Username;

                    _db.ExistingPositions.Update(existingPosition);
                    _db.SaveChanges();


                }

如果我对此进行更新,则抛出CreatedDate不能为null错误,但是如果我取消注释

//existingPosition.CreatedDate = DateTime.Now;
//existingPosition.CreatedBy = _sessionUser.Username;

它将添加一个新行。如何更新当前行?

ViewModel,以防万一出现问题:

public class ExistingPositionGridViewModel
{
    public int Id { get; set; }
    public int FundAgencyId { get; set; }
    public int NoOfPositions { get; set; }
    public decimal? RegularSalary { get; set; }
    public decimal? VacFac { get; set; }
    public decimal? Mappip { get; set; }
    public decimal? Other0111 { get; set; }
    public decimal? Retire { get; set; }
    public decimal? DefComp { get; set; }
    public decimal? Retirement162 { get; set; }
    public decimal? RetireeMed { get; set; }
    public decimal? HealthReimb { get; set; }
    public decimal? PensionPrepmtDisc { get; set; }
    public decimal? Unemployment { get; set; }
    public decimal? SalCont { get; set; }
    public decimal? Health { get; set; }
    public decimal? WellnessProgram { get; set; }
    public decimal? Dental { get; set; }
    public decimal? Life { get; set; }
    public decimal? AdnD { get; set; }
    public decimal? Other0319 { get; set; }
    public decimal? Medicare { get; set; }
    public decimal? TotalSnEb { get; set; }
    public decimal? ExecCar { get; set; }
    public decimal? Obp { get; set; }
    public decimal? GrandTotal { get; set; }
    public string CreatedBy { get; set; }
    public DateTime CreatedDate { get; set; }
    public string ModifiedBy { get; set; }
    public DateTime? ModifiedDate { get; set; }
    public int? FiscalYearId { get; set; }
    public int? DepartmentId { get; set; }
    public int BudgetRequestId { get; set; }
}

我本以为可能是不同的ID,但是我要更新的ID是相同的

1 个答案:

答案 0 :(得分:0)

对于asp.net核心

它要求所有元素都在更新顺序中,包括createdbycreateddate 与此:

_db.ExistingPositions.Add(existingPosition);
_db.Entry(existingPosition).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
_db.SaveChanges();