Entity Framework 4 - Delete Object
- by GibboK
I have 3 Tables in my DataBase
CmsMasterPages
CmsMasterPagesAdvSlots (Pure Juction Table)
CmsAdvSlots
Here a Picture of my EDM:
I need find out all objects CmsAdvSlot connected with a CmsMasterPage (it is working in my code posted belove),
and DELETE the result (CmsAdvSlot) from the DataBase.
My Problem is I am not able to DELETE this Objects when I found theme.
Error: The object cannot be deleted because it was not found in the ObjectStateManager.
int findMasterPageId = Convert.ToInt32(uxMasterPagesListSelector.SelectedValue);
CmsMasterPage myMasterPage = context.CmsMasterPages.FirstOrDefault(x => x.MasterPageId == findMasterPageId);
var resultAdvSlots = myMasterPage.CmsAdvSlots;
// It is working until here
foreach (var toDeleteAdv in resultAdvSlots)
{
context.DeleteObject(myMasterPage.CmsAdvSlots.Any()); // ERORR HERE!!
context.SaveChanges();
}
Any idea how to solve it? Thanks for your time! :-)