EF Update两个未在数据库中更新的实体

时间:2017-06-12 22:04:53

标签: c# asp.net-mvc entity-framework asp.net-mvc-5

我正在尝试更新数据库中的ApplicantApplicantNotification OneToOne关系表。但是,我正在尝试的是不起作用。

我通过EF选择对象,然后覆盖属性,因此我假设EF将跟踪更改,但不会反映在数据库中。

问题: 如何更新EF中的两个实体?

申请人:

[Index]
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int ApplicantID { get; set; }
    [Required]
    public string ApplicantTitle { get; set; }
    [Required]
    public string Firstname { get; set; }
    [Required]
    public string Lastname { get; set; }
    [Required]
    public string Address { get; set; }
    [Required]
    public string Address1 { get; set; }
    [Required]
    public string Address2 { get; set; }
    [Required]
    public string Address3 { get; set; }
    [Required]
    public string Postcode { get; set; }
    [Required]
    public string CaseReference { get; set; }
    [DataType(DataType.Date)]
    public DateTime DateOfBirth { get; set; }
    public ApplicantNotification Notification { get; set; }

ApplicantNotification:

[Index]
        [Key, Column("ApplicantID"), ForeignKey("Applicant")]
        public int ApplicantNotificationID { get; set; }
        public bool FirstNotification { get; set; }
        public bool SecondtNotification { get; set; }
        public bool ThirdNotification { get; set; }
        public bool FinalNotification { get; set; }
        [DataType(DataType.Date)]
        public DateTime? ReminderDate { get; set; }
        public int ReminderFrequency { get; set; }
        [DataType(DataType.Date)]
        public DateTime? FirstNotificationDate { get; set; }
        [DataType(DataType.Date)]
        public DateTime? SecondNotificationDate { get; set; }
        [DataType(DataType.Date)]
        public DateTime? ThirdNotificationDate { get; set; }
        public bool IsArchive { get; set; }
        public virtual Applicant Applicant { get; set; }

方法:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(ApplicantNotificationViewModel model)
{
    if (ModelState.IsValid)
    {
        //select from ef 
        var applicant = (from a in db.Applicants
                         where a.ApplicantID == model.ApplicantID
                         select a).FirstOrDefault();

        var applicantNotification = (from an in db.ApplicantNotifcations
                                     where an.ApplicantNotificationID == model.ApplicantID
                                     select an).FirstOrDefault();

        SetApplicant(model, applicant);
        SetApplicantNotification(model, applicantNotification);

        using (var context = new WorkSmartContext())
        {
                try
                {
                    db.Entry(applicant).State = EntityState.Modified;

                    db.Entry(applicantNotification).State = EntityState.Modified;
                    context.SaveChanges();

                }
                catch (Exception ex)
                {

                }

        }
        return RedirectToAction("Index");
    }
    return View(model);
}

由于

1 个答案:

答案 0 :(得分:0)

你看起来试图越过dbContexts或其他东西,没有看到这个WorkSmartContext定义很难拼凑,但你可能想要的更像是这样:

var applicant = db.Applicants
  .Include(a=>a.Notification)
  .FirstOrDefault(a=>a.ApplicantId = model.ApplicantId);

if (applicant == null)
  // Handle missing Applicant scenario here.

SetApplicant(model, applicant);
SetNotification(model, applicant.Notification);

db.SaveChanges();

您的申请人提及相关通知。用它。您不需要加载个别相关实体。您可以执行诸如检查现有相关实体,更新它们,在必要时插入等操作。如果您需要更新不相关的实体但确保所有实体成功或失败,请使用事务或工作单元模式。 (我猜测这个WorkSmartContext应该是,但不是源于或绑定到" db"我不知道是怎么回事。)

相关问题