asp.net mvc - Deleting records from nested tables MVC 5 -


i having trouble deleting 3 nested tables have.

when try , delete record first table, gives me error

system.data.sqlclient.sqlexception: delete statement conflicted reference constraint

the delete action method :

    [httppost, actionname("delete")]     [validateantiforgerytoken]     public actionresult deleteconfirmed(int id)     {         parent parent = db.parent.firstordefault(t => t.parentid == id);         foreach(var item in parent.child.tolist())         {             db.child.remove(item);             foreach (var grand in item.grand.tolist())             {                 db.grand.remove(grand);             }         }         db.parent.remove(parent);         db.savechanges();         return redirecttoaction("index");     } 

i appreciate on problem also, if can direct me tutorial on crud operations nested tables me out lot.

this working code hyarantar

 public actionresult deleteconfirmed(int id) {     parent parent = db.parent.firstordefault(t => t.parentid == id);     foreach(var item in parent.child.tolist())     {         foreach (var grand in item.grand.tolist())         {             db.grand.remove(grand);         }         db.child.remove(item);       }     db.parent.remove(parent);     db.savechanges();     return redirecttoaction("index"); } 

i needed change order of removing items, db.child.remove(item) has been placed after loop deleting options.


Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -