I have made an ASP.NET Core 3.1
Web-based application and want to use MySQL as the database.
I have been following along with some YouTube tutorials on creating MySQL database
with ASP.NET Core 3.1
[code first approach] including a tutorial from this site:
I have created a DataModel Class
, added a service to UseMySQL
to the Startup.cs Class and created an AppDBContext Class
that implements DbContext Class
.
When I run this command in the Package Manager Console: Add-Migration InitialDatabase
the application is creating a migration successfully.
When I run update-database
it is throwing this exception:
fail: Microsoft.EntityFrameworkCore.Database.Connection[20004]
An error occurred using the connection to database '' on server 'localhost'.
An error occurred using the connection to database '' on server 'localhost'.
System.InvalidOperationException: An exception has been raised that is likely due to a transient failure. Consider enabling transient error resiliency by adding 'EnableRetryOnFailure()' to the 'UseMySQL' call.
When I call the EnableRetryOnFailure();
function as required, I am facing this exception:
fail: Microsoft.EntityFrameworkCore.Database.Connection[20004] An error occurred using the connection to database '' on server 'localhost'. An error occurred using the connection to database '' on server 'localhost'.
What could be the issue? Where am I getting it wrong?
If you have links to useful articles about using MySQL Database
with ASP.NET Core
I would appreciate or your help on this particular issue.
I am using Visual Studio IDE 2019 and ASP.NET Core 3.1.1
Additional Code:
This is the Startup.cs Class:
private IConfiguration _configuration; public Startup(IConfiguration configuration) { _configuration = configuration; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); // database connection string configuration services.AddDbContextPool<AppDBContext>(options => options.UseMySql(_configuration.GetConnectionString("DatabaseConnectionString"), mySqlOptionsAction: options => { options.EnableRetryOnFailure(); } )); services.AddMvc(); }
This is the connection string in appsettings.json:
"ConnectionStrings": { "DatabaseConnectionString": "Server=localhost;Database=MyAppDB;user=root;Password=123;" }