database - How to delete child entities before parent with Entity Framework CF? -


i trying use ef code-first delete db record (deleteme) , it's children (deleteme.prices).

foreach (var deleteme in deletethese) {     // delete validation    if(candeleteitem(deleteme.itemid))    {       db.entry(deleteme).state = entitystate.deleted;        foreach (var item in deleteme.prices)       {          db.entry(item).state = entitystate.deleted; // cascade delete       }    } } db.savechanges(); 

however, entity framework seems unable track fact child records should deleted before parent. error:

the delete statement conflicted reference constraint "itemprice_item".
conflict occurred in database "devdb", table "dbo.itemprices", column 'item_itemid'.
statement has been terminated.

how execute delete in ef?

i ended finding quick line that'd me:

foreach (var deleteme in deletethese) {     // delete validation    if(candeleteitem(deleteme.itemid))    {       ///       deleteme.prices.tolist().foreach(p => db.itemprices.remove(p));       ///        db.entry(deleteme).state = entitystate.deleted;    } } db.savechanges(); 

Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -