2

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

GibboK
  • 71,848
  • 143
  • 435
  • 658

3 Answers3

5

Try this:

while (myMasterPage.CmsAdvSlots.Count > 0)
{
  var slot = myMasterPage.CmsAdvSlots.First();
  myMasterPage.CmsAdvSlots.Remove(slot); // This is required only in some scenarios - it depends on type of entities you are using.
  context.DeleteObject(slot);
}

context.SaveChanges();
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • Hi, i tried but i get an error: Error 1 Cannot apply indexing with [] to an expression of type 'System.Data.Objects.DataClasses.EntityCollection' – GibboK Feb 18 '11 at 09:26
  • @GlbboK: Ok, I modified the example. – Ladislav Mrnka Feb 18 '11 at 09:28
  • Another error now: Unable to update the EntitySet 'CmsMasterPagesAdvSlots' because it has a DefiningQuery and no element exists in the element to support the current operation...... – GibboK Feb 18 '11 at 09:31
  • @GlbboK: There is some problem with your mapping. It says that junction table CmsMasterPagesAdvSlots (table representing M:N relation) is defined by DefiningQuery - this usually happens if you use database view instead of table or proably if your table is read only due to missing primary keys. – Ladislav Mrnka Feb 18 '11 at 09:36
  • My Table CmsMasterPagesAdvSlots is a Many to Many Jucntion Table with NO Primary Key. Do you have any idea how to solve it? Thanks – GibboK Feb 18 '11 at 09:41
  • You must mark both FKs in your junctiona table as complex PK key and update your model. It is required in EF. – Ladislav Mrnka Feb 18 '11 at 09:45
  • Do you mean I need to ALTER the current DataBase adding 2 PK in my Junction Table? And recreate my Model? Correct? Thanks for your time. PS no other work around exist? – GibboK Feb 18 '11 at 09:51
  • @GlbboK: Yes you need to alter current database and add composite PK to junction table. The only work around can be specifying stored procedure for insert, update and delete. EF requires that you define PK for each editable table. – Ladislav Mrnka Feb 18 '11 at 09:53
  • Thank Ladislav Mrnka, In this case I would need use a SPROC mapped in EF. Thanks once again for your help on this. – GibboK Feb 18 '11 at 09:57
  • What about use ATTACHTO Method? Here a resource: http://stackoverflow.com/questions/757067/how-to-delete-many-to-many-relationship-in-entity-framework-without-loading-all-o – GibboK Feb 18 '11 at 10:04
  • @GlbboK: Attaching is only for adding entity to ObjectContext. It itself doesn't perform anything in DB. – Ladislav Mrnka Feb 18 '11 at 10:10
  • Thanks once again for your answer – GibboK Feb 18 '11 at 10:19
0

.Any() returns a boolean. Use .First() instead, .First() will in your case return the first CmsAdvSlot.

If you want to remove all CmsAdvSlot I would follow @Ladislav Mrnka's example.

Filip Ekberg
  • 36,033
  • 20
  • 126
  • 183
  • Hi Filip, now i get an error in the next line "Unable to update the EntitySet 'CmsMasterPagesAdvSlots' because it has a DefiningQuery and no element exists in the element to support the current operation."} any idea? – GibboK Feb 18 '11 at 09:22
  • @GlbboK: So you are working with readonly entities. Are those entities mapped to DB views? – Ladislav Mrnka Feb 18 '11 at 09:24
  • Sorry but i do not understand your question about DB View? Anyway these objects I need to remove are accessible using Navigational Property. any ideas? thanks – GibboK Feb 18 '11 at 09:29
  • For Filip, I tried again your code an now I receive another error: Collection was modified; enumeration operation may not execute – GibboK Feb 18 '11 at 09:30
0

In context.DeleteObject(myMasterPage.CmsAdvSlots.Any());
myMasterPage.CmsAdvSlots.Any() should return you a bool You should try using myMasterPage.CmsAdvSlots.First() instead!

Numan
  • 3,918
  • 4
  • 27
  • 44
  • Hi, i get another error now: Unable to update the EntitySet 'CmsMasterPagesAdvSlots' because it has a DefiningQuery and no element exists in the element to support the current operation."} any idea? – GibboK Feb 18 '11 at 09:23
  • @GIbboK:Are these entities tied to a DB View underneath? – Numan Feb 18 '11 at 09:27
  • Hi, these object are in DataBase CmsMasterPagesAdvSlots, but you can access these object using navigational property. Hope I was able to reply your question. – GibboK Feb 18 '11 at 09:33