Код: Выделить всё
public class ApplicationUserRole : IdentityUserRole
{
public virtual ApplicationUser User { get; set; }
public virtual ApplicationRole Role { get; set; }
public bool IsDefault { get; set; }
public bool IsActive { get; set; }
public bool IsDeactivated { get; set; }
}
Я также расширил IdentityUser следующим образом :
Код: Выделить всё
public class ApplicationUser : IdentityUser
{
public IEnumerable Roles { get; set; }
public ICollection UserRoles { get; set; }
}
Код: Выделить всё
modelBuilder.Entity()
.HasMany(u => u.Roles)
.WithMany(r => r.Users)
.UsingEntity
(au => au.HasOne().WithMany(role => role.UserRoles).HasForeignKey(u => u.RoleId),
au => au.HasOne().WithMany(user => user.UserRoles).HasForeignKey(r => r.UserId))
.ToTable("AspNetUserRoles");
modelBuilder.Entity()
.HasOne(ur => ur.User)
.WithMany(u => u.UserRoles)
.HasForeignKey(ur => ur.UserId);
modelBuilder.Entity()
.HasOne(ur => ur.Role)
.WithMany(r => r.UserRoles)
.HasForeignKey(ur => ur.RoleId);
Код: Выделить всё
var user = await userManager.FindByEmailAsync(username);
if (user != null && await userManager.CheckPasswordAsync(user, password))
{
await _context.Entry(user).Collection(x => x.UserRoles).LoadAsync();
return user;
}
Подробнее здесь: https://stackoverflow.com/questions/790 ... -identityu