public void AddRolePermissions(int RoleId, string Role, List<string> Permissions, int UserId)
        {
            using (SecurityAgencyEntities objContext = new SecurityAgencyEntities())
            {
                RoleViewModel objectRoleViewModel = new RoleViewModel();

                objectRoleViewModel.RoleId = RoleId;
                objectRoleViewModel.Role1 = Role;
                objectRoleViewModel.Last_ModifiedBy = objectRoleViewModel.CreatedBy = UserId;
                objectRoleViewModel.Last_ModifiedDate = objectRoleViewModel.CreatedDate = DateTime.Now;
                objectRoleViewModel.IsDeleted = false;
                objectRoleViewModel.Active = true;
                RoleId = CreateRole(objectRoleViewModel);

                //Removing all previous permissions
                List<RolePermission> objectRolePermission = objContext.RolePermissions.Where(i => i.RoleId == RoleId).ToList();
                objContext.RolePermissions.RemoveRange(objectRolePermission);

                foreach (string strPermission in Permissions)
                {
                    RolePermission objectRolePermissionNew = new RolePermission();
                    objectRolePermissionNew.RoleId = RoleId;
                    objectRolePermissionNew.PermissionId = Convert.ToInt32(strPermission.Replace("chk_", ""));
                    objectRolePermissionNew.CreatedBy = UserId;
                    objectRolePermissionNew.CreatedDate = DateTime.Now;
                    objectRolePermissionNew.IsDeleted = false;
                    objContext.RolePermissions.Add(objectRolePermissionNew);
                }
                objContext.SaveChanges();
            }
        }
        public int CreateRole(RoleViewModel roleViewModel)
        {
            Role role = null;

            if (roleViewModel.RoleId > 0)
            {
                role = _repository.Find<Role>(x => x.RoleId == roleViewModel.RoleId);
                if (role == null)
                    return 0;

                role.Role1 = roleViewModel.Role1;
                role.ModifiedBy = roleViewModel.Last_ModifiedBy;
                role.ModifiedDate = DateTime.Now;

                _repository.Modify<Role>(role);
                return role.RoleId;
            }

            Mapper.CreateMap<RoleViewModel, Role>();
            role = Mapper.Map<RoleViewModel, Role>(roleViewModel);

            role.CreatedDate = DateTime.Now;
            role.CreatedBy = roleViewModel.CreatedBy;
            role.Active = true;
            role.IsDeleted = false;

            _repository.Insert<Role>(role);
            return role.RoleId;
        }