Ich habe versucht, den gesamten Code für alle Benutzer mit ihren Rollen zu verwenden, sodass ich meinen Code ein wenig ändern musste und auf diesen Fehler gestoßen bin. Ich bin nicht sicher, was ich falsch gemacht habe, ich habe es auf meine startup.cs und ApplicationDBContect
Klasse eingegrenzt . Ich habe keine Fehler und benötige möglicherweise eine Migration. Ich habe dies nicht getan, um weitere Probleme zu vermeiden.
Ich verweise auf die Stackoverflow-Frage und hatte andere Fehler.
ApplicationDBContext.cs
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string, IdentityUserClaim<string>,
ApplicationUserRole, IdentityUserLogin<string>,
IdentityRoleClaim<string>, IdentityUserToken<string>>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<ApplicationUserRole>(userRole =>
{
userRole.HasKey(ur => new { ur.UserId, ur.RoleId });
userRole.HasOne(ur => ur.Role)
.WithMany(r => r.UserRoles)
.HasForeignKey(ur => ur.RoleId)
.IsRequired();
userRole.HasOne(ur => ur.User)
.WithMany(r => r.UserRoles)
.HasForeignKey(ur => ur.UserId)
.IsRequired();
});
}
public DbSet<ApplicationUser> ApplicationUser { get; set; }
}
Startup.cs
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultUI()
.AddDefaultTokenProviders();
Ich sehe , Sie sind beide erstreckt IdenityUser
und IdentityRole
mit ApplicationUser
und ApplicationRole
jeweils aber du hast sie nicht in Ihrer Identität Service - Registrierung hinzufügen. Aktualisieren Sie daher Ihre Identitätsdienstregistrierung beim Start wie folgt:
services.AddIdentity<ApplicationUser, ApplicationRole>() // </-- here you have to replace `IdenityUser` and `IdentityRole` with `ApplicationUser` and `ApplicationRole` respectively
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultUI()
.AddDefaultTokenProviders();
Dieser Artikel stammt aus dem Internet. Bitte geben Sie beim Nachdruck die Quelle an.
Bei Verstößen wenden Sie sich bitte [email protected] Löschen.
Lass mich ein paar Worte sagen