Error on EF migration - Azure - MySql on App
Error on EF migration - Azure - MySql on App
I'm trying to run all pending migrations on my web app asp.net core 2.1 on Azure using MySql on App, free tier (always on not enabled) but I receive an error, as in the log file:
"Microsoft.EntityFrameworkCore.Database.Connection: An error occurred using the connection to database '' on server '127.0.0.1:54184'"
The mysql package I'm using is Pomelo.EntityFrameworkCore.MySql, version 2.1.1.
I'm running this code in the Configure method of the Startup class:
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
using (var context = serviceScope.ServiceProvider.GetService<SandroDbContext>())
{
context.Database.Migrate();
}
}
It seems to get the correct server and port from the environment variable for MySql in App, but not the database name.
UPDATE:
It doesn't work even if i remove the lines of code to apply migrations.
Simply by injecting the DbContext I have the same error.
It may be because when the web app starts, the Sql in App isn't yet available.
1 Answer
1
It seems the problem is the connection string that Azure provides via environment variable. It misses the parameter "Port", because the port is next to the server name, but the connection string is invalid this way. I've made a few lines of code to fix it.
public string FixAzureConnectionString(string connStr)
{
string goodConnStr = connStr;
string portNumber = Regex.Match(goodConnStr, @"(?<=Data Source.+:)d+")?.Value;
goodConnStr += ";Port=" + portNumber;
goodConnStr = goodConnStr.Replace(":" + portNumber, "");
return goodConnStr;
}
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.