InvalidOperationException when registering a new user with ASP .NET Core Identity and EntityFrameworkCore

后端 未结 2 1761
耶瑟儿~
耶瑟儿~ 2021-01-18 21:09

I\'m following the documentation for using Identity and am trying register a new user (executing the register action), but it fails with the following error:

相关标签:
2条回答
  • 2021-01-18 21:43

    I found the problem. My ApplicationContext was inheriting from DbContext. I changed it to IdentityDbContext<ApplicationUser> and it works.

    0 讨论(0)
  • 2021-01-18 22:10

    Create new context class which inherit IdentityDbContext.

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }
    
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            // Customize the ASP.NET Identity model and override the defaults if needed.
            // For example, you can rename the ASP.NET Identity table names and more.
            // Add your customizations after calling base.OnModelCreating(builder);
        }
    }
    

    and in startup.cs file add below code

    services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(connection));
    

    This will help you for database first approach.

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