C# EF更新数据库中未更新的两个实体
我正在尝试更新数据库中的C# EF更新数据库中未更新的两个实体,c#,asp.net-mvc,entity-framework,asp.net-mvc-5,C#,Asp.net Mvc,Entity Framework,Asp.net Mvc 5,我正在尝试更新数据库中的applicator和applicationnotificationOneToOne关系表。然而,我正在尝试的是不工作 我通过EF选择对象,然后覆盖属性,因此我假设EF将跟踪更改,但不会反映回数据库中 问题: 如何更新EF中的两个实体 申请人: [Index] [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int ApplicantID { g
applicator
和applicationnotification
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);
}
谢谢您似乎试图跨越DBContext或其他内容,但没有看到此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”派生或绑定到“db”,我不知道是怎么回事。)没有,我也尝试过,没有更新表为什么在这种情况下事务?EF已将其包装为一个。请参阅调用SaveChanges()一次。这就是它成为交易的原因。阅读我的链接。谢谢您的尝试!!!:)@MatheusMirandaChange context.SaveChanges()到db.SaveChanges()真是太棒了,我想知道如何使用导航属性来实现这一点