I'm having an issue with an Azure SQL DB (SQL PaaS) connection string in my Azure Web App. It is defined in the "Connection Strings" settings of the Application Config. I see the Conn String value (format below) in https://myAppName.scm.azurewebsites.net/Env.cshtml#connectionStrings. My defined Conn String name is "MyApp" and Azure is prefixing it with "SQLAZURECONNSTR_" as you'd expect.
SQLAZURECONNSTR_MyApp = Server=tcp:mysvr.database.windows.net,1433;Initial Catalog=MyApp;Persist Security Info=False;User ID=user@mysvr;Password=passWord;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;
In Startup.cs, I have this though I'm not certain it is necessary.
public void ConfigureServices(IServiceCollection services)
{
[...]
services.AddDbContext<MyAppContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("MyApp")));
And in my database context class, I have the following:
public partial class MyAppContext : DbContext
{
[...]
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.Development.json", optional: true, reloadOnChange: true)
.Build();
var connectionString = configuration.GetConnectionString("MyApp");
optionsBuilder.UseSqlServer(connectionString);
}
}
}
When accessing the web app executing a method that connects to the database, I'm receiving a null reference to the conn string.
ArgumentNullException: Value cannot be null. (Parameter 'connectionString')
Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty(string value, string parameterName)
Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseSqlServer(DbContextOptionsBuilder optionsBuilder, string connectionString, Action<SqlServerDbContextOptionsBuilder> sqlServerOptionsAction)
myapp.Models.MyAppContext.OnConfiguring(DbContextOptionsBuilder optionsBuilder) in MyAppContext.cs
No conn strings are defined in the local .json file, though I'll want to add that back for development purposes at some point.