Home
Help
Register
Log in

Search

 
   Active Threads  

You are here: Home > LLBLGen Pro > LLBLGen Pro Runtime Framework> Error Message when adding range to an entity collection
 

Pages: 1
LLBLGen Pro Runtime Framework
Error Message when adding range to an entity collection
Page:1/1 

  Print all messages in this thread  
Poster Message
fpw2377
User



Location:
Houston TX
Joined on:
23-Feb-2007 15:30:54
Posted:
35 posts
# Posted on: 29-Mar-2007 21:19:52.  
I am trying to manage users and the roles they belong but I am getting this error message when I try to update their role mappings:

Collection was modified; enumeration operation may not execute.

I am using llblgen 2.0 v2.0.50727

I have the database setup like this

User table
Id,
Name

Role table
Id,
Name

UserRoles Table
UserID,
RoleID

What I do is display a list of all availiable roles to the screen as a checked list, before rendering I look at the users rolemappings(UserRoles Table Entires) and then check those. After the edit is done I call the UserEntity and clear out its RoleMappings then add back the new roles selected during the edit. Here is the code

Code:

this._sb_localEntity.RoleMappings.Clear();
this._sb_localEntity.RoleMappings.AddRange(this.ctrlUserRoles.CurrentUserRoles);



The currentUserRoles is an EntityCollection<> of RoleMappings.

The clear works but the add back gets me the error message above. Here is the stack trace:

Quote:

at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
at System.Collections.Generic.List`1.Enumerator.MoveNext()
at SD.LLBLGen.Pro.ORMSupportClasses.CollectionCore`1.AddRange(ICollection`1 c)
at PetRescueCentral.Web.Controls.Users.AddEditUser.UnBind() in C:\source\PetRescueCentral\trunk\WebSite\Controls\Users\AddEditUser.ascx.cs:line 171
at PetRescueCentral.Web.Controls.Users.AddEditUser.cmdSave_Click(Object sender, ImageClickEventArgs e) in C:\source\PetRescueCentral\trunk\WebSite\Controls\Users\AddEditUser.ascx.cs:line 252
at System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e)
at System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument)
at System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,


Not sure if there is a better way to approach this, I am open to any suggestions.

Thanks,

Frank
  Top
bclubb
User



Location:
Norman, Oklahoma
Joined on:
12-Feb-2004 22:18:04
Posted:
934 posts
# Posted on: 30-Mar-2007 02:15:45.  
If you are using a foreach to process the entity collection then you cannot modify the collection inside the loop. If you post more of the code that is being executed we may be able to give you a better recommendation.

  Top
fpw2377
User



Location:
Houston TX
Joined on:
23-Feb-2007 15:30:54
Posted:
35 posts
# Posted on: 30-Mar-2007 05:32:55.  
What I posted is the code, I am not doing any foreach loops. I am calling clear on a typed entitycollection then trying to add new records back to it using the AddRange method. According to the help, addrange can take another entity collection of the same type. so here is my process step by step

1) get a list of new roles for the user as a typed entitycollection of UserRoleEntities

2) clear out the existing typed entity collection or UserRoleEntities using the .Clear() method.

3) add the new roles from step one back into the typed entity collection from step 2. This is done using the .AddRange() method which takes a typed entity collection of UserRoleEntities.


The part that is failing is step 3, step two works fine. My goal in all this is to be able to save the new roles to the database. The save action may have to delete existing roles that were removed during the clear in step 2 that did not come back in the addrange in step 3. Will that work, if I delete an entity from an entity collection then save that collection will the record in the database be deleted as well?

hope all that makes sense,


Thanks,

Frank
  Top
Walaa
Support Team



Location:

Joined on:
21-Aug-2005 16:03:48
Posted:
14569 posts
# Posted on: 30-Mar-2007 09:25:19.  
Quote:
if I delete an entity from an entity collection then save that collection will the record in the database be deleted as well?

No that won't happen.

Either explicitly Delete the entities instead of/before calling clear.
Or Use a UnitOfWork object to hold the entities to be deleted and hold the entities to be saved and then commit the UOW.

Quote:
3) add the new roles from step one back into the typed entity collection from step 2. This is done using the .AddRange() method which takes a typed entity collection of UserRoleEntities.

The part that is failing is step 3

Are you sure that RoleMappings is a collection of UserRole entities, I have a feeling that it's a collection of Role entities.


  Top
fpw2377
User



Location:
Houston TX
Joined on:
23-Feb-2007 15:30:54
Posted:
35 posts
# Posted on: 30-Mar-2007 15:11:34.  
Ok, I see, what I am trying to do will not work like I thought. I was trying to cheat and do it the easy way Regular Smiley

I am sure the RoleMappings is the correct entity. I have the tables setup so that I can have a user belong to multiple roles so there is that table between users and roles that makes the relationships. It only has a UserID and RoleID column. When I look at the user entity in code, I see a collection of the roles and a collection of the role mappings. Now from what I understand if I want to add/remove a role to the user I have to use the role mappings collection, I can't just add/remove in roles collection. Is that correct?

  Top
Walaa
Support Team



Location:

Joined on:
21-Aug-2005 16:03:48
Posted:
14569 posts
# Posted on: 30-Mar-2007 16:12:24.  
Quote:
Now from what I understand if I want to add/remove a role to the user I have to use the role mappings collection, I can't just add/remove in roles collection. Is that correct?

Yes that's correct.

The manual has a relevant example: check the answer of the How do I create a m:n relation between two entity objects? question in the How do I...? section in the LLBLGen Pro manual under "Best practises -> How do I ... ?"


  Top
fpw2377
User



Location:
Houston TX
Joined on:
23-Feb-2007 15:30:54
Posted:
35 posts
# Posted on: 30-Mar-2007 20:15:04.  
So if I have a UserRole table that has 2 roles in it for the user and I pull that back as a userentity that has a property that is a userroles entitycollection with those two roles in it, if i delete both roles from that entitycollection and do a recursive save on the userentity, will that delete the role records from the database or will I need to delete them one by one?
  Top
bclubb
User



Location:
Norman, Oklahoma
Joined on:
12-Feb-2004 22:18:04
Posted:
934 posts
# Posted on: 31-Mar-2007 03:03:12.  
None of the deletes will be recursive. So you will either need the use the DBs cascade ability to delete or delete the related records by manually.

  Top
Pages: 1  


Powered by HnD ©2002-2007 Solutions Design
HnD uses LLBLGen Pro

Version: 2.1.12172008 Final.