0

I have StudentRegistration Model Class as follows

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

I was trying to delete existing studentreceipt list and add new list.The new studentreceipt list is adding properly in the database but the existing studentreceipt list is not getting deleted from the database and the StudentRegistrationId of existing studentreceipt list is set to null.

I want to delete the existing studentreceipt list from the database and add new list.How can I do it ?

This is what I have tried

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);
    }
  }
}
13
  • As I have mentioned above the new studentreceipt list is adding properly in the database but the existing studentreceipt list is not getting deleted from the database .Infact the StudentRegistrationId of existing studentreceipt list is set to null. Commented Oct 19, 2016 at 14:08
  • So are there any StudentReceipts in _dbRegn.StudentReceipts.ToList()? Commented Oct 19, 2016 at 14:13
  • Yes there will be StudentReceipts in _dbRegn.StudentReceipts.ToList().And I want to delete the existing list and add the new list Commented Oct 19, 2016 at 14:16
  • As @MarkC. suggest, if lazy loading is disabled (I suppose that), when you execute this _dbRegn = _db.StudentRegistrations.Where(r => r.Id == Id).FirstOrDefault(); you are not including the StudentReceipts list property. So you are removing nothing. Commented Oct 19, 2016 at 14:16
  • @JoseAlonsoMonge As you can see I am using virtual keyword for Student Receipt Collection.So I think include is not required here Commented Oct 19, 2016 at 14:20

1 Answer 1

1

You can try as shown below.

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);
    }
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your response mate.I have a small doubt , if accidentally something went wrong after removal of studentreceipt and before addinng new receipt,will the existing receipt will get deleted or it will get deleted only after _ts.Complete()
It will happen after _ts.Complete().If any one of the operation fails then all operations will be rolled back.that is the key advantage of using transactions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.