I have an ASP .NET app that use MySQL instead of SQL Server. This app run perfectly on development environment (Windows machine), both on Debug and Release.
However when I publish and deploy on a Ubuntu Server machine (linux-arm64, self-contained) and run it, I got NRE in which MySQL connectionString
is null.
Here is the relevant part in Startup.cs
:
//string mySqlConnectionStr = Configuration.GetConnectionString("DefaultConnection");
// NOTE: hard-code because connection string is null on linux;
const string mySqlConnectionStr = "Server=localhost; port=3306; Database=aspnet_p24_mysql; user=root; password=root; Persist Security Info=False; Connect Timeout=300";
services.AddDbContext<ApplicationDbContext>(options =>
options.UseMySql(mySqlConnectionStr, opts => opts.ServerVersion(ServerVersion.AutoDetect(mySqlConnectionStr)))
);
And here is appsettings.json
, even though I tried hard-code the connection string:
{
"ConnectionStrings": {
"DefaultConnection": "Server=localhost; port=3306; Database=aspnet_p24_mysql; user=root; password=root; Persist Security Info=False; Connect Timeout=300"
},
...
}
I did search around and found some related questions/answers, but none matched my situation, since I didn't make any typo on ConnectionStrings
, and the issue only happen on the deployed ubuntu machine.