Entity Framework 4 - Delete Object

Posted by GibboK on Stack Overflow See other posts from Stack Overflow or by GibboK
Published on 2011-02-18T09:15:22Z Indexed on 2013/10/18 15:55 UTC
Read the original article Hit count: 166

I have 3 Tables in my DataBase

CmsMasterPages
CmsMasterPagesAdvSlots (Pure Juction Table)
CmsAdvSlots

Here a Picture of my EDM:

enter image description here

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! :-)

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET