Possible Duplicate:
Delete object and all its child objects in Entity Framework?
This code:
int WebsiteID = int.Parse(Request.QueryString["id"]);
Website websiteObj = db.Websites
.SingleOrDefault(x => x.website_id == WebsiteID);
foreach (BusinessObjects.Page pageObj in websiteObj.Pages)
{
foreach (SubPage subpageObj in pageObj.SubPages)
{
pageObj.SubPages.Remove(subpageObj);
}
websiteObj.Pages.Remove(pageObj);
}
foreach (Sector sectorObj in websiteObj.Sectors)
{
foreach (Product productObj in sectorObj.Products)
{
sectorObj.Products.Remove(productObj);
}
websiteObj.Sectors.Remove(sectorObj);
}
db.Websites.DeleteObject(websiteObj);
A website has multiple pages, a page has multiple subpages. A website also has multiple sectors and each sector has multiple products.
I want to delete the website and clear all relationships + entities related to it. I'm sure there are better ways to write the above.
Is there way to improve the logic?