【问题标题】:Update Database - Deleting existing data and adding new data with entityframework更新数据库 - 删除现有数据并使用实体框架添加新数据
【发布时间】:2016-10-19 14:04:27
【问题描述】:

我有如下 StudentRegistration 模型类

public partial class StudentRegistration
{
    public StudentRegistration()
    {            
        this.StudentReceipts = new HashSet<StudentReceipt>();            
    }

    public int Id { get; set; }
    public Nullable<int> StudentWalkInnID { get; set; } 
    ...
    ...  

    public virtual ICollection<StudentReceipt> StudentReceipts { get; set; }

}
public partial class StudentReceipt
{
    public int Id { get; set; }
    public Nullable<int> StudentRegistrationID { get; set; }
    ...
    ...    

    public virtual StudentRegistration StudentRegistration { get; set; }
}

我试图删除现有的学生收据列表并添加新列表。new studentreceipt list 已正确添加到数据库中,但 existing studentreceipt list 未从数据库中删除,existing studentreceipt listStudentRegistrationId 设置为null.

我想从数据库中删除现有的studentreceipt list 并添加新列表。我该怎么做?

这是我尝试过的

using (TransactionScope _ts = new TransactionScope())
{
  _dbRegn = _db.StudentRegistrations
    .Where(r => r.Id == Id).FirstOrDefault();
  if (_dbRegn != null)
  {                      
    //Remove existing receipts
    foreach (var _existingReceipt in _dbRegn.StudentReceipts.ToList())
    {
      _dbRegn.StudentReceipts.Remove(_existingReceipt);
    }                           

    //adding new receipt
    foreach (var _receipt in mdlCourseInterchange.StudentReceiptList)
    {
      StudentReceipt _studReceipt = new StudentReceipt();
      //...
      //...
      _dbRegn.StudentReceipts.Add(_studReceipt);
    }
    //...
    //..

    db.Entry(_dbRegn).State = EntityState.Modified;
    int j = _db.SaveChanges();
    if (j > 0)
    {
      _ts.Complete();
      return Json(new { message = "success" }, JsonRequestBehavior.AllowGet);
    }
  }
}

【问题讨论】:

  • 正如我上面提到的,新的 studentreceipt 列表正在数据库中正确添加,但现有的 studentreceipt 列表没有从数据库中删除。事实上,现有 studentreceipt 列表的 StudentRegistrationId 设置为 null。跨度>
  • 那么StudentReceipts中有_dbRegn.StudentReceipts.ToList()吗?
  • 是的,StudentReceipts 中会有_dbRegn.StudentReceipts.ToList()。我想删除现有列表并添加新列表
  • 作为@MarkC。建议,如果禁用延迟加载(我想是这样),当您执行此 _dbRegn = _db.StudentRegistrations.Where(r =&gt; r.Id == Id).FirstOrDefault(); 时,您不包括 StudentReceipts 列表属性。所以你什么都没有删除。
  • @JoseAlonsoMonge 如您所见,我使用virtual keyword 代替Student Receipt Collection。所以我认为这里不需要include

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


【解决方案1】:

你可以试试如下图。

using (TransactionScope _ts = new TransactionScope())
{
  _dbRegn = _db.StudentRegistrations.Where(r => r.Id == Id).FirstOrDefault();

  if (_dbRegn != null)
  {                      
    //Remove existing receipts
    foreach (var _existingReceipt in _dbRegn.StudentReceipts.ToList())
    {
      __db.StudentReceipts.Remove(_existingReceipt);
    }                           

    //adding new receipt
    foreach (var _receipt in mdlCourseInterchange.StudentReceiptList)
    {
      StudentReceipt _studReceipt = new StudentReceipt();
      //...
      //...
      _db.StudentReceipts.Add(_studReceipt);
    }
    //...
    //..

    int j = _db.SaveChanges();
    if (j > 0)
    {
      _ts.Complete();
      return Json(new { message = "success" }, JsonRequestBehavior.AllowGet);
    }
  }
}

【讨论】:

  • 感谢您的回复,我有一个小疑问,如果在removal of studentreceipt and before addinng new receipt之后出现意外问题,existing receipt会被删除还是只有在_ts.Complete()之后才会被删除跨度>
  • 它会在_ts.Complete()之后发生。如果任何一个操作失败,那么所有操作都会回滚。这是使用事务的关键优势。
猜你喜欢
  • 2020-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-24
  • 1970-01-01
相关资源
最近更新 更多