1

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.

David Liang
  • 20,385
  • 6
  • 44
  • 70

1 Answers1

0

NEWEST

Code First.

Database Initialization in Entity Framework 6

connectionStr should be like this.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings>
    <add name="SchoolDBConnectionString" connectionString="tcp:servername.database.windows.net,1433;Initial Catalog=db; Persist Security Info=False; User ID=user;Password=mypassword; MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" providerName="System.Data.SqlClient"/>
    </connectionStrings>
</configuration>

PRIVIOUS

DB First.

Your project use ef core, so the connectionstrings should be like below.

<connectionStrings>
<add name="SchoolDBEntities" connectionString="metadata=res://*/SchoolDB.csdl|res://*/SchoolDB.ssdl|res://*/SchoolDB.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\sqlexpress;initial catalog=SchoolDB;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient"/>
</connectionStrings>

For more details, you can refer my answer in the post.

Timeout period elasped prior to obtaining a connection from the pool - Entity Framework

You can also refer to this tutorial, which will help you.

Entity Framework Tutorial

Jason Pan
  • 15,263
  • 1
  • 14
  • 29
  • That conn string is only valid for DB first models. I'm using code first. The conn string as-is in my post works given I'm running it on my local dev machine provided it is in appsettings.json. –  Nov 10 '20 at 13:44