'C# - Entity Framework Core - auto create database

I want to automatically create the database and table structures when my desktop app is first run.
I can't find any examples or documentation on something equivalent for EFCore.
I did everything as it is in this documentation: https://dev.mysql.com/doc/connector-net/en/connector-net-entityframework-core-example.html
But I still have a problem: Exception thrown: 'Microsoft.Data.SqlClient.SqlException' in Microsoft.Data.SqlClient.dll, and empty database :( Please help me, if you can.

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {

            try
            {
                optionsBuilder.UseMySQL("server=localhost;userid=root;password=topSecret;database=kartoteka-pracownicza");               
            }
            catch (MySqlException ex)
            {
                switch (ex.Number)
                {
                    case 0:
                        MessageBox.Show("Cannot connect to server.  Contact administrator");
                        break;

                    case 1045:
                        MessageBox.Show("Invalid username/password, please try again");
                        break;
                }
            };
        }


Solution 1:[1]

Hello You can add to something like this in your Startup.cs if you using Migrations

public void Configure(IApplicationBuilder app)
    {
         using (var serviceScope = 
         app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
         {
               var context = serviceScope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
               context.Database.Migrate();
         }
    }

If you are not using Migrations you can change Migrate(); with EnsureCreated(); for using your DB context

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1