【问题标题】:EF Update Two entities not updating in databaseEF 更新两个实体未在数据库中更新
【发布时间】:2017-06-12 22:04:53
【问题描述】:

我正在尝试更新数据库中的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; }

申请人通知:

[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);
}

谢谢

【问题讨论】:

  • 不,我也试过了,不会更新表格
  • 为什么是这种场景下的事务? EF 已经将它包装成一个。见here
  • 调用 SaveChanges() 一次。这就是使它成为交易的原因。阅读我的链接。
  • 感谢您的尝试!!! :) @MatheusMiranda
  • 将 context.SaveChanges() 更改为 db.SaveChanges()

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


【解决方案1】:

您似乎正在尝试跨 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”,我不明白这是怎么回事。)

【讨论】:

  • 太棒了,想知道如何使用导航属性来做到这一点
猜你喜欢
  • 2022-11-13
  • 1970-01-01
  • 2021-10-28
  • 1970-01-01
  • 2019-09-16
  • 2018-08-14
  • 2015-01-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多