0

I have two model classses ; SecurityRole & SecurityRoleTypePermission. where each Securityrole can have zero to many SecurityRoleTypePermission. now i have the following repository method, with the aim to delete the associated SecurityRole & all of its SecurityRoleTypePermissions if they are avilable:- i tried the following code :-

public void DeleteRole(int id, string username)
        {
            var role = tms.SecurityRoles.SingleOrDefault(a=>a.SecurityRoleID ==id);
            var auditinfo = IntiateAdminAudit(tms.AuditActions.SingleOrDefault(a => a.Name.ToUpper() == "DELETE").ID, tms.SecurityTaskTypes.SingleOrDefault(a => a.Name.ToUpper() == "SECURITY ROLE").ID, username, role.Name);
            var srtp = role;
            foreach (var i in srtp.SecurityroleTypePermisions)
            {
                tms.SecurityroleTypePermisions.Remove(i);
            }
            tms.SecurityRoles.Remove(role);

            InsertOrUpdateAdminAudit(auditinfo);
        }

But i got the following exception :-

Collection was modified; enumeration operation may not execute.

so me question is why i am getting this error, although i have copied the role object to a new srtp variable ?

Second question , i need to know if there is a way to delete an object navigation property, without the need to loop though each record using the foreach as in my code ?

Thanks

John John
  • 1
  • 72
  • 238
  • 501

0 Answers0