Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Professional ASP.NET MVC 2
BOOK: Professional ASP.NET MVC 2
This is the forum to discuss the Wrox book Professional ASP.NET MVC 2 by Jon Galloway, Scott Hanselman, Phil Haack, Scott Guthrie, Rob Conery; ISBN: Professional ASP.NET MVC 2
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional ASP.NET MVC 2 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old February 16th, 2011, 08:19 PM
Registered User
Join Date: Jan 2011
Posts: 4
Thanks: 0
Thanked 1 Time in 1 Post
Smile Exception error "Collection was modified; enumeration operation may not execute"

Hi folks.

For anyone out there who tried to exercise the delete action method when the user is already authenticated.

In case you have succeeded after all printing errors in the book and tries to follow the advice in the last paragraph of page 116 in Chapter 1: "We can repeat the same steps for the Delete action methods within our Controller to lock down permission to delete Dinners as well, and ensure that only the host of a Dinner can delete it."

You will find that the code defined for Delete a dinner in the repository (DinnerRepository) will generate an exception "Collection was modified; enumeration operation may not execute" error.

This is due to the fact that as you delete the objects from the context, the Entity Framework is actively updating the RSVPs navigation property count which means the "dinner.RSVPs" collection is being changed during the "foreach" loop which will always cause the exception you are getting.

So the are 2 possible solutions:

a) Change the current foreach parameters:

public void Delete(Dinner dinner)

//--- Instead of using "foreach ( var rsvp in dinner.RSVPs )" ---//
//--- use this: ---//
foreach ( var rsvp in dinner.RSVPs.ToList() )
entities.RSVPs.DeleteObject( rsvp );



b) Use the LINQ ForEach method instead:

public void Delete(Dinner dinner)

//--- Instead of using "foreach ( var rsvp in dinner.RSVPs )" ---//
//--- use this: ---//
dinner.RSVPs.ToList().ForEach(record =>


Hope it helps (at least it worked for me )

The Following User Says Thank You to caof2005 For This Useful Post:
BrennanFuchs (April 6th, 2011)
Old April 6th, 2011, 07:26 PM
Registered User
Join Date: Apr 2011
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts

Thanks mate... VERY green to both C# and MVC heck still stuck on 2.0 at work so even the framework is newish. Knew why the error was occurring not idea what the solution was.

Similar Threads
Thread Thread Starter Forum Replies Last Post
MSXSL gives error message for "for" inside "select" ilyaz XSLT 1 December 9th, 2010 05:02 PM
Need code corrections "get sub or function not defined" error" bluesboytoo Excel VBA 1 October 22nd, 2009 11:12 PM
How to theme the "Browse" button of "FileUpload" control? varunbwj BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 October 14th, 2009 01:22 AM
Add a CheckBox DataColumn to my DataGridView, Null format: "" or "True" but Error: F ismailc C# 2005 0 September 25th, 2009 04:56 AM
Code not going as planned: "icicle" vs "savedinstancestate" joopthecat BOOK: Professional Android Application Development ISBN: 978-0-470-34471-2 3 May 3rd, 2009 03:09 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.