Removing a RoleAssignment RRS feed

  • Question

  • In my application I have a self defined screen for managing users and roles. When I save changes I want to synchronize my user/role relationships with the role assignments of the user at the LightSwitch security level.

    Therefor I wrote the following code:

    partial void Benutzer_Updating( Benutzer entity )
                var reg = ( from regs in this.DataWorkspace.SecurityData.UserRegistrations
                            where regs.UserName == entity.UserName
                            select regs ).FirstOrDefault();

                if( reg != null )
                    // Update FullName
                    reg.FullName = entity.FullName;

                    // -- update role assignments
                    // 1. Remove assignments from SecurityData
                    var listDelRoles = new List<RoleAssignment>();

                    foreach( var role in reg.RoleAssignments )
                        var localAssignedRole = entity.Benutzer_MPRoleCollection.SingleOrDefault( r => r.MPRole.RoleName == role.RoleName );

                        if( localAssignedRole == null )
                            listDelRoles.Add( role );

                    foreach( var role in listDelRoles )
                        reg.RoleAssignments.Remove( role );

                    // 2. Create new assignments in SecurityData
                    foreach( var localAssignedRole in entity.Benutzer_MPRoleCollection )
                        var roleAssigned = reg.RoleAssignments.FirstOrDefault( r => r.RoleName == localAssignedRole.MPRole.RoleName );

                        if( roleAssigned == null )
                            var newRoleAssignment = reg.RoleAssignments.AddNew();
                            newRoleAssignment.User = reg;
                            newRoleAssignment.Role = this.DataWorkspace.SecurityData.Roles_Single( localAssignedRole.MPRole.RoleName );


    Creating new role assignments works fine, but removing an assignment by reg.RoleAssignments.Remove( role ) fires the following exception:

    System.InvalidOperationException wurde nicht von Benutzercode behandelt.
      Message=Der Wert der Eigenschaft "User" kann nicht festgelegt werden, da sie schreibgeschützt ist.
           bei Microsoft.LightSwitch.Details.Framework.Base.EntityDetails`2.SetReferenceValue[T](TEntity entity, Entry entry, T value)
           bei Microsoft.LightSwitch.Details.Framework.Base.EntityReferencePropertyBase`3.Entry.SetValueCore(TDetails details, Object value)
           bei Microsoft.LightSwitch.Details.Framework.Base.PropertyObject`2.Entry.Microsoft.LightSwitch.Details.Internal.IPropertyEntry.SetValue(IDetails details, Object value)
           bei Microsoft.LightSwitch.Framework.Internal.EntityCollectionBase`3.Remove(TReferencedEntity entity)
           bei LightSwitchApplication.ApplicationDataService.Benutzer_Updating(Benutzer entity)
           bei LightSwitchApplication.ApplicationDataService.DetailsClass.__Benutzer_Updating(ApplicationDataService d, Benutzer e)
           bei Microsoft.LightSwitch.Details.Framework.Server.EntitySetEntry`2.Microsoft.LightSwitch.Details.Framework.Server.IEntitySetEntry.InvokeUpdating(IDataService dataService, IEntityObject entity)
           bei Microsoft.LightSwitch.ServerGenerated.Implementation.DataServiceImplementation`1.<>c__DisplayClass43.<PerformPreEvent>b__3e(IEntitySetEntry entitySetEntry)
           bei Microsoft.LightSwitch.ServerGenerated.Implementation.DataServiceImplementation`1.<>c__DisplayClass46.<CallPrePostUserCode>b__45()
           bei Microsoft.LightSwitch.Utilities.Internal.UserCodeHelper.CallUserCode(Type sourceType, String methodName, String instance, String operation, ILoggingContext context, Action action, String additionalText, Func`1 getCompletedMessage, Boolean swallowException, Exception& exception)

    How can I remove role assignments by code?







    • Moved by Steve HoagModerator Tuesday, July 26, 2011 4:44 PM new forum (From:Visual Studio LightSwitch - General (Beta))
    Monday, July 18, 2011 3:07 PM


  • I think this worked for me. Save the securityData workspace from your calling method after.

        public static bool RemoveUserFromRole(DataWorkspace ws, string userName, string roleName)
          var ra = ws.SecurityData.RoleAssignments_SingleOrDefault(userName, roleName);
          if (ra == null) return false;
          return true;

    Tuesday, July 26, 2011 5:07 PM