hi .I have problem when I want update relation between Role and permission Entities in Seed invariant data . in first time when app run , the default invariant roles with themselves permission insert to db but if second time when developing project I want add new permission to permission list of one role in invariant data then this new permission actually was inserted into db by other permission just I want set relation between in existing permission with other role , but the relation don't update !!!!!!
var systemRoleNames = SystemRoleNames.GetStandardRoles();
foreach (var role in systemRoleNames)
{
var systemRole = _roles.AsNoTracking().FirstOrDefault(a => a.Name == role.Name) ?? new ApplicationRole
{
Description = role.Description,
Name = role.Name,
IsActive = true,
IsDefaultForRegister = role.Name == SystemRoleNames.Registered.Name,
IsSystemRole = true
};
var defaultPermissionsRecord = SystemPermissionsProvider.GetInvariatSystemPermissionsWithRole()
.FirstOrDefault(a => a.SystemRoleName == role.Name);
systemRole.Permissions = new List<ApplicationPermission>();
if (defaultPermissionsRecord != null)
{
var actualPermissions = _permissionService.GetActualPermissions(
defaultPermissionsRecord.Permissions);
foreach (var permission in actualPermissions)
{
systemRole.Permissions.Add(permission);
}
}
try
{
_unitOfWork.Update(systemRole, a => a.OwnedCollection(b => b.Permissions));
_unitOfWork.SaveChanges();
}
finally
{
_unitOfWork.AutoDetectChangesEnabled = true;
_unitOfWork.ValidateOnSaveEnabled = true;
_unitOfWork.ProxyCreationEnabled = true;
}
}
}
this is getActuallPermissions in PermissionService
public IEnumerable GetActualPermissions(List permissions)
{
var permissionNames = permissions.Select(a => a.Name).ToArray();
var inDbPermissions = _permissions.Where(a => permissionNames.Any(p => p == a.Name)).ToList();
var result = new List<ApplicationPermission>(inDbPermissions);
var noInDbPermissions = permissions.Where(a => inDbPermissions.All(p => p.Name != a.Name)).ToList();
result.AddRange(noInDbPermissions);
return result;
}
GetAllPermissions
region GetSystemPermissions
/// <summary>
/// get all permissions in system
/// </summary>
/// <returns></returns>
public static List<ApplicationPermission> GetSystemPermissions()
{
return new List<ApplicationPermission>
{
CanViewRoles,
CanCreateRole,
CanEditRole,
CanSetRoleForRegister,
CanDeleteRole,
CanCreateUser,
CanViewUsers,
CanEditUser,
CanDeleteUser,
CanManageUserSetting,
CanVisitAdminPanel,
CanLogOff,
};
}
#endregion
region GetInvariatSystemPermissionsWithRole
/// <summary>
/// Get Default Permissions with own role for seed database
/// </summary>
/// <returns></returns>
public static IList<DefaultPermissionsRecord> GetInvariatSystemPermissionsWithRole()
{
return new List<DefaultPermissionsRecord>
{
new DefaultPermissionsRecord
{
SystemRoleName = SystemRoleNames.SuperAdministrators.Name,
Permissions = GetSystemPermissions()
},
new DefaultPermissionsRecord
{
SystemRoleName = SystemRoleNames.Administrators.Name,
Permissions = new List<ApplicationPermission>
{
CanCreateRole,
CanViewUsers,
CanEditUser,
CanManageUserSetting,
CanVisitAdminPanel,
CanLogOff
}
},
new DefaultPermissionsRecord
{
SystemRoleName = SystemRoleNames.BlogModerators.Name,
Permissions = new List<ApplicationPermission>
{
CanViewUsers,
CanVisitAdminPanel,
CanLogOff
}
}
,
new DefaultPermissionsRecord()
{
SystemRoleName = SystemRoleNames.Registered.Name,
Permissions = new List<ApplicationPermission>
{
CanLogOff
}
}
};
}
#endregion
public static IEnumerable GetStandardRoles()
{
return new List
{
SuperAdministrators,
Administrators,
BlogModerators,
Registered
};
}
public static DefaultRoleRecord SuperAdministrators = new DefaultRoleRecord
{
Name = "SuperAdmins",
Description = "مدیران ارشد"
};
public static DefaultRoleRecord Administrators = new DefaultRoleRecord
{
Name = "Administrators",
Description = "مدیران"
};
public static DefaultRoleRecord BlogModerators = new DefaultRoleRecord
{
Name = "BlogModerators",
Description = "مدیران وبلاگ"
};
public static DefaultRoleRecord Registered = new DefaultRoleRecord
{
Name = "Registered",
Description = "کاربران عضو شده"
};
}
hi .I have problem when I want update relation between Role and permission Entities in Seed invariant data . in first time when app run , the default invariant roles with themselves permission insert to db but if second time when developing project I want add new permission to permission list of one role in invariant data then this new permission actually was inserted into db by other permission just I want set relation between in existing permission with other role , but the relation don't update !!!!!!
this is my seed method I invoke in Startup.cs
public void SeedDatabase() { _unitOfWork.AutoDetectChangesEnabled = false; _unitOfWork.ValidateOnSaveEnabled = false; _unitOfWork.ProxyCreationEnabled = false;
this is getActuallPermissions in PermissionService
public IEnumerable GetActualPermissions(List permissions)
{
var permissionNames = permissions.Select(a => a.Name).ToArray();
GetAllPermissions
region GetSystemPermissions
region GetInvariatSystemPermissionsWithRole
public static IEnumerable GetStandardRoles()
{
return new List
{
SuperAdministrators,
Administrators,
BlogModerators,
Registered
};
}
public static DefaultRoleRecord SuperAdministrators = new DefaultRoleRecord
{
Name = "SuperAdmins",
Description = "مدیران ارشد"
};
i############# this my project https://github.com/rabbal/AspNetMVCNTierTemp thanks.