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

后端 未结 22 1869
时光取名叫无心
时光取名叫无心 2020-12-13 10:00

I had the following error in package manager console when Add-Migration

Value cannot be null. Parameter name: connectionString

相关标签:
22条回答
  • 2020-12-13 10:01

    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

    0 讨论(0)
  • 2020-12-13 10:03

    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");
    }
    
    0 讨论(0)
  • 2020-12-13 10:03

    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.

    0 讨论(0)
  • 2020-12-13 10:05

    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

    0 讨论(0)
  • 2020-12-13 10:06

    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.

    0 讨论(0)
  • 2020-12-13 10:06

    My problem was when I was trying to run App.dll within netcoreapp2.1 folder, but the right folder is netcoreapp2.1\publish\

    0 讨论(0)
提交回复
热议问题