title: The User table is not created in MySql database using Entity Framework Core 3.1 (Code-first approach )
body: <p>How can I create user table in MySql database using Code First approach and Entity Framework Core 3.1?</p>
  
  <pre><code>"DataAccessMySqlProvider": "server=localhost;database=jahan_alpha;uid=xxxx;password=xxxx" in appsettings.json
  </code></pre>
  
  <p>And in startup.cs:</p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
     services.AddDbContext&lt;ApplicationDbContext&gt;(options =&gt;
              options.UseMySQL(Configuration["ConnectionStrings:DataAccessMySqlProvider"],
              b =&gt; b.MigrationsAssembly("Alpha.DataAccess")));
     // .UseMySQL is in Microsoft.EntityFrameworkCore namespace
  }
  </code></pre>
  
  <p>I run <code>Add-Migration Init-MySql-database</code> and then  <code>Update-database</code> in Package Manager Console.
  After doing that, I got this error:</p>
  
  <pre><code>PM&gt; Update-Database Build started... Build succeeded.
  Microsoft.EntityFrameworkCore.Infrastructure[10403]
        Entity Framework Core 3.1.3 initialized 'ApplicationDbContext' using provider 'Pomelo.EntityFrameworkCore.MySql' with options:
  MigrationsAssembly=Alpha.DataAccess 
  Microsoft.EntityFrameworkCore.Database.Command[20101]
        Executed DbCommand (1,299ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
        CREATE DATABASE `jahan_alpha`; Microsoft.EntityFrameworkCore.Database.Command[20101]
        Executed DbCommand (9,281ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
        CREATE TABLE `__EFMigrationsHistory` (
            `MigrationId` varchar(95) NOT NULL,
            `ProductVersion` varchar(32) NOT NULL,
            CONSTRAINT `PK___EFMigrationsHistory` PRIMARY KEY (`MigrationId`)
        ); Microsoft.EntityFrameworkCore.Database.Command[20101]
        Executed DbCommand (118ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
        SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='jahan_alpha' AND TABLE_NAME='__EFMigrationsHistory';
  Microsoft.EntityFrameworkCore.Database.Command[20101]
        Executed DbCommand (5ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
        SELECT `MigrationId`, `ProductVersion`
        FROM `__EFMigrationsHistory`
        ORDER BY `MigrationId`; Microsoft.EntityFrameworkCore.Migrations[20402]
        Applying migration '20200515231101_Init-MySql'. Applying migration '20200515231101_Init-MySql'. fail:
  Microsoft.EntityFrameworkCore.Database.Command[20102]
        Failed executing DbCommand (181ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
        ALTER TABLE `User` DROP INDEX `UserNameIndex`; Failed executing DbCommand (181ms) [Parameters=[], CommandType='Text',
  CommandTimeout='30'] ALTER TABLE `User` DROP INDEX `UserNameIndex`;
  MySql.Data.MySqlClient.MySqlException (0x80004005): Table
  'jahan_alpha.user' doesn't exist  ---&gt;
  MySql.Data.MySqlClient.MySqlException (0x80004005): Table
  'jahan_alpha.user' doesn't exist
      at MySqlConnector.Core.ResultSet.ReadResultSetHeaderAsync(IOBehavior ioBehavior) in C:\projects\mysqlconnector\src\MySqlConnector\Core\ResultSet.cs:line 49
      at MySql.Data.MySqlClient.MySqlDataReader.ActivateResultSet() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlDataReader.cs:line 130
      at MySql.Data.MySqlClient.MySqlDataReader.CreateAsync(CommandListPosition commandListPosition, ICommandPayloadCreator payloadCreator, IDictionary`2 cachedProcedures, IMySqlCommand command, CommandBehavior behavior, IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlDataReader.cs:line 391
      at MySqlConnector.Core.CommandExecutor.ExecuteReaderAsync(IReadOnlyList`1 commands, ICommandPayloadCreator payloadCreator, CommandBehavior behavior, IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\Core\CommandExecutor.cs:line 62
      at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQueryAsync(IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlCommand.cs:line 226
      at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlCommand.cs:line 74
      at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQuery(RelationalCommandParameterObject parameterObject)
      at Microsoft.EntityFrameworkCore.Migrations.MigrationCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
      at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable`1 migrationCommands, IRelationalConnection connection)
      at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
      at Pomelo.EntityFrameworkCore.MySql.Migrations.Internal.MySqlMigrator.Migrate(String targetMigration)
      at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
      at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String contextType)
      at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.&lt;&gt;c__DisplayClass0_0.&lt;.ctor&gt;b__0()
      at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
  Table 'jahan_alpha.user' doesn't exist
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/tPD7r.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tPD7r.jpg" alt="jahan_alpha.user&#39; doesn&#39;t exist"></a></p>
  
  <p><strong>ApplicationDbContext:</strong></p>
  
  <pre><code>namespace Alpha.DataAccess
  {
      public class ApplicationDbContext : IdentityDbContext&lt;User, Role, int, UserClaim, UserRole, UserLogin, RoleClaim, UserToken&gt;
      {
          public ApplicationDbContext(DbContextOptions&lt;ApplicationDbContext&gt; options) : base(options)
          {
  
          }
          // some codes...
  
          protected override void OnModelCreating(ModelBuilder modelBuilder)
          {
              // it should be placed here, otherwise it will rewrite the following settings!
              base.OnModelCreating(modelBuilder);
  
              modelBuilder.Entity&lt;RoleClaim&gt;(builder =&gt;
              {
                  builder.HasOne(roleClaim =&gt; roleClaim.Role).WithMany(role =&gt; role.Claims).HasForeignKey(roleClaim =&gt; roleClaim.RoleId);
                  builder.ToTable("RoleClaim");
              });
              modelBuilder.Entity&lt;Role&gt;(builder =&gt;
              {
                  builder.ToTable("Role");
              });
              modelBuilder.Entity&lt;UserClaim&gt;(builder =&gt;
              {
                  builder.HasOne(userClaim =&gt; userClaim.User).WithMany(user =&gt; user.Claims).HasForeignKey(userClaim =&gt; userClaim.UserId);
                  builder.ToTable("UserClaim");
              });
              modelBuilder.Entity&lt;UserLogin&gt;(builder =&gt;
              {
                  builder.HasOne(userLogin =&gt; userLogin.User).WithMany(user =&gt; user.Logins).HasForeignKey(userLogin =&gt; userLogin.UserId);
                  builder.ToTable("UserLogin");
              });
  
              modelBuilder.Entity&lt;User&gt;(builder =&gt;
              {
                  builder.ToTable("User"); //.HasMany(e =&gt; e.Comments).WithOne().OnDelete(DeleteBehavior.Cascade);
              });
              modelBuilder.Entity&lt;UserRole&gt;(builder =&gt;
              {
                  builder.HasOne(userRole =&gt; userRole.Role).WithMany(role =&gt; role.Users).HasForeignKey(userRole =&gt; userRole.RoleId);
                  builder.HasOne(userRole =&gt; userRole.User).WithMany(user =&gt; user.Roles).HasForeignKey(userRole =&gt; userRole.UserId);
                  builder.ToTable("UserRole");
              });
              modelBuilder.Entity&lt;UserToken&gt;(builder =&gt;
              {
                  builder.HasOne(userToken =&gt; userToken.User).WithMany(user =&gt; user.UserTokens).HasForeignKey(userToken =&gt; userToken.UserId);
                  builder.ToTable("UserToken");
              });
  
              // some codes...
          }
  
          public static async Task CreateAdminAccount(IServiceProvider serviceProvider, IConfiguration configuration)
          {
              UserManager&lt;User&gt; userManager = serviceProvider.GetRequiredService&lt;UserManager&lt;User&gt;&gt;();
              RoleManager&lt;Role&gt; roleManager = serviceProvider.GetRequiredService&lt;RoleManager&lt;Role&gt;&gt;();
  
              string userName = configuration["Data:AdminUser:Name"];
              string email = configuration["Data:AdminUser:Email"];
              string password = configuration["Data:AdminUser:Password"];
              string role = configuration["Data:AdminUser:Role"];
  
              if (await userManager.FindByNameAsync(userName) == null)
              {
                  if (await roleManager.FindByNameAsync(role) == null)
                  {
                      await roleManager.CreateAsync(new Role(role));
                  }
  
                  User user = new User
                  {
                      Email = email,
                      UserName = userName
                  };
                  var result = userManager.CreateAsync(user, password);
                  if (result.IsCompletedSuccessfully)
                  {
                      await userManager.AddToRoleAsync(user, role);
                  }
              }
          }
      }
  }
  </code></pre>