ASP.NET Core 2.1 - IdentityUser Issue - Cannot create a DbSet for 'IdentityUser' this type is not included in the model for the context

后端 未结 4 1834
再見小時候
再見小時候 2021-01-17 10:34

I have upgraded my code from ASP.NET Core 2.0 to Core 2.1. I created a new Core 2.1 project and moved my code into the new project. I have provided samples of my startup and

相关标签:
4条回答
  • 2021-01-17 10:51

    Try changing public partial class ApplicationDbContext : IdentityDbContext<ApplicationUser> to public partial class ApplicationDbContext : IdentityDbContext<IdentityUser>

    Compiler will generate DbSet with the type provided to generic IdentityDbContext<TUser> class.

    0 讨论(0)
  • 2021-01-17 10:52

    Change the ApplicationDbContext to:

    private static bool _Created = false;
    
    public ApplicationDbContext()
    {
        if (!_Created)
        {
            _Created = true;
            Database.EnsureCreated();
    
        }
    }
    
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(@"server = .\SQLSERVER; initial catalog = DBName; Integrated Security = True; MultipleActiveResultSets = True; App = EntityFramework & quot; ");
    }
    
    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
    }
    
    0 讨论(0)
  • 2021-01-17 10:55

    From Your startup.cs change

    services.AddDefaultIdentity<IdentityUser>()
    

    To

    services.AddDefaultIdentity<ApplicationUser>()
    
    0 讨论(0)
  • 2021-01-17 11:12

    As a follow up: to avoid the next possible issue as soon as this here is fixed: You also have to change the types in the Views\Shared_LoginPartial.cshtml

    From

    @inject SignInManager<IdentityUser> SignInManager
    @inject UserManager<IdentityUser> UserManager
    

    To

    @inject SignInManager<ApplicationUser> SignInManager
    @inject UserManager<ApplicationUser> UserManager
    
    0 讨论(0)
提交回复
热议问题