1

In my application, users are able to be assigned roles. In the setup page for this, I have the following code:

foreach (string userRole in roleArray)
{
    OrganizationRole orgRole = signedUpOrg.Roles.FirstOrDefault(target => target.Name == userRole && target.OrganizationId == signedUpOrg.OrganizationId);
    if (orgRole != null)
    {
        OrganizationUser_OrganizationRole existingUserRole = orgRole.OrganizationUser_OrganizationRole.FirstOrDefault(target => target.Organization_User.User.UserId    == orgUser.User.UserId &&
                                                                                                                                target.OrganizationRole.Name            == userRole && 
                                                                                                                                target.OrganizationRole.OrganizationId  == signedUpOrg.OrganizationId);

        if (existingUserRole == null || orgUser.User.UserId == 0) // new user role to new users or existing users with new roles
        {
            orgRole.OrganizationUser_OrganizationRole.Add(new OrganizationUser_OrganizationRole
            {
                Organization_User   = orgUser,
                OrganizationRole    = orgRole
            });
        }
    }
}

With this, we are able to cycle through the roles which are to be assigned and save them in the database. This works perfectly fine on creating the user and their roles, but on editing there is no change. The code seems to be hit at all the crucial points with the correct data (roles, etc) but there is no reflection in the database or on the front end.

This code is in a method called SaveUsers in a class called CommonUtilities and is called in an AdministrationController with the following code:

CommonUtilities.SaveUsers(viewModel);

Can anyone possibly think of any reasons as to why this would work correctly on creation but not while editing? Many thanks in advance and I will be more than willing ot clarifiy on any points.

Nick Chambers
  • 181
  • 1
  • 13
  • Is this the same code you are using when creating a new user? or this is just for updating? – SOfanatic May 10 '13 at 13:16
  • Can you provide the code in SaveUsers method? You are using EF or ADO? – Jhonatas Kleinkauff May 10 '13 at 13:16
  • @SOfanatic This is the same code that is run for creating and updating users in a Quick Account Setup page that can be used by my admin user. It works for creating, but not for updating. – Nick Chambers May 10 '13 at 13:21
  • @JhonatasKleinkauff That extract above is in fact from the SaveUsers method, I can post the entire class if you would like? And we are using EF. – Nick Chambers May 10 '13 at 13:22
  • @JhonatasKleinkauff Here is the entire method: http://codeviewer.org/view/code:32ff – Nick Chambers May 10 '13 at 13:29
  • 1
    @NickChambers i didn't see where you are calling the SaveChanges of your context. But ok, look if this links can help you http://stackoverflow.com/questions/4728212/entity-framework-attach-update-not-working AND http://stackoverflow.com/questions/4075107/entity-framework-poco-savechanges-on-update-doesnt-work – Jhonatas Kleinkauff May 10 '13 at 13:37
  • 1
    Just like @JhonatasKleinkauff said, not sure how your method is successfully adding new users, there doesn't seem to be a `context.SaveChanges()` being called anywhere. – SOfanatic May 10 '13 at 13:55
  • @JhonatasKleinkauff My bad, Jhonatas, I should have been pointing towards this: http://codeviewer.org/view/code:3300 As you can see, we are applying SetAsModified at some stages, but perhaps you can noticed a missing Object State change? Thanks for all your help. – Nick Chambers May 10 '13 at 14:01

1 Answers1

1

A similar issue drove me to choose NHibernate over EF as it can update children and grandchildren collections (probably even deeper levels but I have not tried that). Adjusting ObjectState like the answers referred to by @JhonatasKleinkauff seemed the only answer in EF but was not satisfactory in our case. This seems to only happen when you disconnect from the ObjectContext between retrieval and saving the parent object.

JTMon
  • 3,189
  • 22
  • 24
  • Yes, this seems to be an issue since we upgraded our EF not too long ago, so I'm not sure if we were just far behind the times or if this is a recent change, but it is certainly an irritation! We do appear to be applying "SetAsModified" in some instances (as my latest comment shows) but maybe we have missed one necessary for this. Thanks for your help! – Nick Chambers May 10 '13 at 14:03