I customized some functionalities of Identity system to fit my needs.
I have the following ApplicationUserManager:
public class ApplicationUserManager : UserManager<ApplicationUser, long>
{
public bool AddUserToRole(long userId, string roleName)
{
return this.AddToRole(userId, roleName).Succeeded;
}
public bool ClearAllUserRoles(long userId)
{
foreach (var role in this.GetRoles(userId))
{
if (!this.RemoveFromRole(userId, role).Succeeded)
{
return false;
}
}
return true;
}
}
And the other custom classes are:
public class ApplicationUser : IdentityUser<long, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
public virtual UserInfo UserInfo { get; set; }
}
public class ApplicationRole : IdentityRole<long, ApplicationUserRole>
{
public ApplicationRole() : base() { }
public ApplicationRole(strin
{
}
public virtual string Description { get; set; }
}
public class ApplicationUserRole : IdentityUserRole<long> { }
Adding users and adding them to roles seems to be working fine. My problem is when I want to remove a user from roles.
When I want to Add user to a list of roles, I start clearing the user from all roles that he is already in with the method `ClearAllUserRoles(long userId)` and after I add him to each selected role.
My problem is that the `RemoveFromRole(userId, role)` method used in `ClearAllUserRoles(long userId)` only puts the fields `ApplicationUser_Id` and `ApplicationRole_Id` of my `AspNetUserRoles` table to null, leaving the fields `UserId` and `RoleId` filled so when I try to reassign the user to a role that he was already in the following exception is thrown:
{"Violation of PRIMARY KEY constraint 'PK_dbo.AspNetUserRoles'. Cannot insert duplicate key in object 'dbo.AspNetUserRoles'. The duplicate key value is (16, 1).\r\nThe statement has been terminated."}
This is my `OnModelCreating()`method:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
EntityTypeConfiguration<ApplicationUser> table =
modelBuilder.Entity<ApplicationUser>().ToTable("AspNetUsers");
modelBuilder.Entity<ApplicationUser>().HasMany<ApplicationUserRole>((ApplicationUser u) => u.Roles);
modelBuilder.Entity<ApplicationUserRole>().HasKey((ApplicationUserRole r) =>
new { UserId = r.UserId, RoleId = r.RoleId }).ToTable("AspNetUserRoles");
EntityTypeConfiguration<ApplicationRole> entityTypeConfiguration1 =
modelBuilder.Entity<ApplicationRole>().ToTable("AspNetRoles");
entityTypeConfiguration1.Property((ApplicationRole r) => r.Name).IsRequired();
}
And in my Migration, the create table is like this:
CreateTable(
"dbo.AspNetUserRoles",
c => new
{
UserId = c.Long(nullable: false),
RoleId = c.Long(nullable: false),
ApplicationUser_Id = c.Long(),
ApplicationRole_Id = c.Long(),
})
.PrimaryKey(t => new { t.UserId, t.RoleId })
.ForeignKey("dbo.AspNetUsers", t => t.ApplicationUser_Id)
.ForeignKey("dbo.AspNetRoles", t => t.ApplicationRole_Id)
.Index(t => t.ApplicationUser_Id)
.Ind;x(t => t.ApplicationRole_Id);
My Questions are:
1. Should my `AspNetUserRoles` table have those 4 columns (`ApplicationUser_Id`, `ApplicationRole_Id`, `UserId`,`RoleId`) or I am doing something wrong in the `OnModelCreating()`method of my database context?
2. Is the `RemoveFromRole(userId, role)` method working as expected or it should delete the all row of `AspNetUserRoles` table?