更新数据库 - 删除现有数据并使用 entityframework 添加新数据
Update Database - Deleting existing data and adding new data with entityframework
我的 StudentRegistration 模型 Class 如下
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; }
}
我试图删除现有的学生收据列表并添加新的 list.The new studentreceipt list
正在正确添加到数据库中,但是 existing studentreceipt list
没有从数据库中删除并且 [= existing studentreceipt list
的 15=] 设置为 null
。
我想从数据库中删除现有的 studentreceipt list
并添加新的 list.How 可以吗?
这是我试过的
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);
}
}
}
您可以尝试下图
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);
}
}
}
我的 StudentRegistration 模型 Class 如下
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; }
}
我试图删除现有的学生收据列表并添加新的 list.The new studentreceipt list
正在正确添加到数据库中,但是 existing studentreceipt list
没有从数据库中删除并且 [= existing studentreceipt list
的 15=] 设置为 null
。
我想从数据库中删除现有的 studentreceipt list
并添加新的 list.How 可以吗?
这是我试过的
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);
}
}
}
您可以尝试下图
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);
}
}
}