Can't inherit from IdentityUser and IdentityRole in ASP.NET Core 2.0

后端 未结 5 2227
一向
一向 2021-02-13 00:51

I\'m trying to finish updating to .NET Core 2.0 but a couple of errors pop up:

The problem:

I have two classes, ApplicationRole and ApplicationU

5条回答
  •  迷失自我
    2021-02-13 01:04

    ICollection> Roles , ICollection> Claims and ICollection> Logins navigation properties have been removed from Microsoft.AspNetCore.Identity.IdentityUser .

    You should define them manually

    public class MyUser : IdentityUser
    {                
        public virtual ICollection> Roles { get; } = new List>();
    
        public virtual ICollection> Claims { get; } = new List>();
    
        public virtual ICollection> Logins { get; } = new List>();
    }
    

    To prevent duplicate foreign keys when running EF Core Migrations, add the following to your IdentityDbContext class' OnModelCreating method

    protected override void OnModelCreating(ModelBuilder builder)
    {
      base.OnModelCreating(builder);
    
    builder.Entity()
        .HasMany(e => e.Claims)
        .WithOne()
        .HasForeignKey(e => e.UserId)
        .IsRequired()
        .OnDelete(DeleteBehavior.Cascade);
    
    builder.Entity()
        .HasMany(e => e.Logins)
        .WithOne()
        .HasForeignKey(e => e.UserId)
        .IsRequired()
        .OnDelete(DeleteBehavior.Cascade);
    
    builder.Entity()
        .HasMany(e => e.Roles)
        .WithOne()
        .HasForeignKey(e => e.UserId)
        .IsRequired()
        .OnDelete(DeleteBehavior.Cascade);
    }
    

    Migrating Authentication and Identity to ASP.NET Core 2.0

提交回复
热议问题