asp.net core 2.0 - Value cannot be null. Parameter name: connectionString

I found my own problem.

I have an AppDbContextFactory class which inherits IDesignTimeDbContextFactory. Deleting this class resolves this issue.


This problem occurred when the connection string can't be found.

Probably you have the following code in Startup class:

public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<BenchmarkContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("yourConnectionString name from appsettings.json")));
    }

These methods solve your problem:

1- Instead of Configuration.GetConnectionString("yourConnectionString name from appsettings.json(in develop mode: 'appsettings.Development.json')") just put your connectionstring.

public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<BenchmarkContext>(options =>
  options.UseSqlServer("Data Source=.;Initial Catalog=Benchmark;Persist Security Info=True;User ID=****;Password=****"));
    }

2- If you are going to use the Configuration file add these codes to Startup class:

public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }
public IConfiguration Configuration;

public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<BenchmarkContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("TestConnection")));
    }

Appsettings.json file(in develop mode: 'appsettings.Development.json'):

{
  "ConnectionStrings": {
    "TestConnection": "Data Source=.;Initial Catalog=Benchmark;Persist Security Info=True;User ID=****;Password=****"
  }
}

After that execute 'add-migration name' command in Package Manager Console


I had such issue when load tesing the service (I recommend it to all) and had ~3/1000 requests with errors, so I changed

services.AddDbContextPool<AppDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

to

string connectionString = Configuration.GetConnectionString("DefaultConnection");
services.AddDbContextPool<AppDbContext>(options =>
options.UseSqlServer(connectionString));

So it reads connections string 1 time and doesn't use Configuration on every request. And now 100% requests are successful. But it seems to be a bug in .Net Core


I had the same issue, but my solution was a lot simpler. All I did was to change the order of the appsettings.json from:

{
  "Message": "Hello World",
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=NotMyFault;Trusted_Connection=True;MultipleActiveResultSets=true"
  }
}

to:

{
   "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=NotMyFault;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
  "Message": "Hello World"
}

I have a suspicion that there is a sequence/order of parameters in the appsettings.json file.