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

后端 未结 5 2232
一向
一向 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<IdentityUserRole<int>> Roles , ICollection<IdentityUserClaim<int>> Claims and ICollection<IdentityUserLogin<int>> Logins navigation properties have been removed from Microsoft.AspNetCore.Identity.IdentityUser .

    You should define them manually

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

    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<MyUser>()
        .HasMany(e => e.Claims)
        .WithOne()
        .HasForeignKey(e => e.UserId)
        .IsRequired()
        .OnDelete(DeleteBehavior.Cascade);
    
    builder.Entity<MyUser>()
        .HasMany(e => e.Logins)
        .WithOne()
        .HasForeignKey(e => e.UserId)
        .IsRequired()
        .OnDelete(DeleteBehavior.Cascade);
    
    builder.Entity<MyUser>()
        .HasMany(e => e.Roles)
        .WithOne()
        .HasForeignKey(e => e.UserId)
        .IsRequired()
        .OnDelete(DeleteBehavior.Cascade);
    }
    

    Migrating Authentication and Identity to ASP.NET Core 2.0

    0 讨论(0)
  • 2021-02-13 01:12

    Solved.

    The package that was keeping those classes Microsoft.AspNetCore.Identity.EntityFrameworkCore changed. To access those clases (IdentityUser and IdentityRole) one must add

    using Microsoft.AspNetCore.Identity;
    

    With this, the problem is gone.

    0 讨论(0)
  • 2021-02-13 01:12

    You need to install Microsoft.AspNetCore.Identity.EntityFramework package

    Install-Package Microsoft.AspNetCore.Identity.EntityFramework
    

    or

    dotnet add package Microsoft.AspNetCore.Identity.EntityFramework
    
    0 讨论(0)
  • 2021-02-13 01:28

    For me it got solved by installing package:

    Microsoft.Extensions.Identity.Stores
    
    0 讨论(0)
  • 2021-02-13 01:30

    This happen to me but I was using Asp.net Core with traditional .NET Framework. You need to add the Nuget package Microsoft.Extensions.Identity. That contains the classes IdentityUser and IdentityRole.

    I think the reason you have to add this is when your project isn't using the Microsoft.AspNetCore.All package.

    0 讨论(0)
提交回复
热议问题