c# - Delete Cascade on Code First - One-to–Zero-or-One -


i have entity named items, have several entities have foreign key (not nullable) table key itemid.

then have table named soldiers depends on items, in case it's (nullable) foreign key.

when delete item on items table error regarding items exist on soldiers table. if don't have soldiers items don't error, , other tables correctly cascade deleted.

i guess have put on fluent api cascade delete on table, not sure how it.

thanks

you can configure withcascadeondelete.

modelbuilder.entity<item>()    .hasmany(i => i.soldiers)    .withoptional(s => s.item)    .hasforeignkey(s => s.itemid)    .willcascadeondelete(true); 

or

modelbuilder.entity<soldier>()    .hasoptional(s => s.item)    .withmany() // -> use => i.soldiers if    .hasforeignkey(i => i.itemid)    .willcascadeondelete(true); 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -