06 July 2017

When you connect to the database and it throws: "ArgumentNullException: Value cannot be null. Parameter name: connectionString"

Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseSqlServer(DbContextOptionsBuilder optionsBuilder, string connectionString, Action<SqlServerDbContextOptionsBuilder> sqlServerOptionsAction)
SampleProject.Startup.<ConfigureServices>b__3_0(DbContextOptionsBuilder options) in Startup.cs

It only means you're trying to connect to the database without first "building" the configuration of the appsettings.json with the hosting environment.

The solution to this is to add the following to your Startup class:

         public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
            Configuration = builder.Build();
         }  

No comments:

Post a Comment