Entity framework, remove entity if it detached from another entity -


i have model relation:

modelbuilder.entity<product>()       .hasmany(p => p.properties)       .withoptional(); 

for examle, product1 have 3 property, , when remove 1 property product (not db) want delete in db, because not used anywhere anymore. can using ef?

if navigational properties not virtual:

using(var db = new yourdbcontext() { var product = db.products.firstordefault(x => ...); product.properties.removeall(x => ...); db.savechanges(); } 

otherwise:

using(var db = new yourdbcontext() { var product = db.products.include("properties").firstordefault(x => ...); product.properties.removeall(x => ...); db.savechanges(); } 

Comments