应用程序上EF迁移-Azure-MySql时出错
我正在尝试在Azure上使用MySql应用程序上的免费层(始终打开未启用)在我的web应用程序asp.net core 2.1上运行所有挂起的迁移,但我收到一个错误,如日志文件中所示:应用程序上EF迁移-Azure-MySql时出错,mysql,azure,migration,Mysql,Azure,Migration,我正在尝试在Azure上使用MySql应用程序上的免费层(始终打开未启用)在我的web应用程序asp.net core 2.1上运行所有挂起的迁移,但我收到一个错误,如日志文件中所示: "Microsoft.EntityFrameworkCore.Database.Connection: An error occurred using the connection to database '' on server '127.0.0.1:54184'" 我使用的mysql包是Pomelo.Ent
"Microsoft.EntityFrameworkCore.Database.Connection: An error occurred using the connection to database '' on server '127.0.0.1:54184'"
我使用的mysql包是Pomelo.EntityFrameworkCore.mysql,版本2.1.1。
我正在Startup类的Configure方法中运行以下代码:
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
using (var context = serviceScope.ServiceProvider.GetService<SandroDbContext>())
{
context.Database.Migrate();
}
}
使用(var serviceScope=app.ApplicationServices.GetRequiredService().CreateScope())
{
使用(var context=serviceScope.ServiceProvider.GetService())
{
Migrate();
}
}
它似乎从应用程序中MySql的环境变量中获得了正确的服务器和端口,但没有获得数据库名称
更新:
即使我删除代码行以应用迁移,它也不起作用。
仅仅通过注入DbContext,我就有了相同的错误。
这可能是因为当web应用程序启动时,应用程序中的Sql尚未可用。问题似乎是Azure通过环境变量提供的连接字符串。它忽略了参数“Port”,因为该端口位于服务器名称旁边,但连接字符串以这种方式无效。我已经编写了几行代码来修复它
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;
}
公共字符串FixAzureConnectionString(字符串connStr)
{
字符串goodConnStr=connStr;
字符串portNumber=Regex.Match(goodConnStr,@“(?)?