我有一个MVC编辑页面,我也在其上编辑相关数据。
在回发时,我有以下代码来处理相关数据的插入,更新和删除:
if (ModelState.IsValid)
{
Guid myGuid = new Guid();
//-- Set PK and FK values --
foreach (AccountType_Organisatie myAccType_Org in myOrg.AccountType_Organisaties)
{
if (myAccType_Org.AccountType_Organisatie_GUID == myGuid)
{
//-- Create --
myAccType_Org.AccountType_Organisatie_GUID = Guid.NewGuid();
myAccType_Org.Organisatie_GUID = myOrg.Organisatie_GUID;
csContext.Entry(myAccType_Org).State = EntityState.Added;
}
else
{
//-- Update --
//-- Force MOdified state - even when unchanged --
csContext.Entry(myAccType_Org).State = EntityState.Modified;
}
//myAccType_Org.AccountType = csContext.AccountTypes.Find(myAccType_Org.Accounttype_GUID);
}
//-- Explicitly handle Deletes --
List<Guid> lGuids = myOrg.AccountType_Organisaties.Select(p => p.AccountType_Organisatie_GUID).ToList();
csContext.AccountType_Organisaties.RemoveRange(csContext.AccountType_Organisaties.Where(p => p.Organisatie_GUID == myOrg.Organisatie_GUID).Where(q => !lGuids.Contains(q.AccountType_Organisatie_GUID)));
csContext.Entry(myOrg).State = EntityState.Modified;
csContext.SaveChanges();
return RedirectToAction("Index");
}
对于如何实施上述内容的一些帮助/指导表示赞赏。