I had the following error in package manager console when Add-Migration
Value cannot be null. Parameter name: connectionString
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
If you are using an IDesignTimeDbContextFactory, you will need to add a default constructor to it with no parameters. Try something like this:
public DbContextFactory()
{
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddEnvironmentVariables()
.AddJsonFile("appsettings.json", false, true)
.Build();
_connectionString = configuration.GetConnectionString("ConnectionStringName");
}
I had this problem due to a difference in connectionstring
of appsetting.json
file, and the GetConnectionString(connectionstrings)
parameter in startup.cs
. Once I removed extra s
in startup.cs
, the problem disappeared.
This problem occurred when the connectionstring can't be found.
Probably you have following codes 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")
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")));
}
Appsetting.json file:
{
"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 the same problem, because I was using the default value in Startup.cs. I just edited Configuration property from:
public IConfiguration Configuration { get; }
to:
public IConfiguration Configuration;
and it worked! If someone say why would be appreciated.
My problem was when I was trying to run App.dll within netcoreapp2.1 folder, but the right folder is netcoreapp2.1\publish\