简体   繁体   中英

Why do I get this error: System.ArgumentNull Exception: Value cannot be null. (Parameter 'connection String') ASP.NET Core

Tools: Visual Studio 2019, Asp.net core 5.0 (AnyCpu), Windows 7 x64

I'm developing a small app and if I start it from VS or click on it it works fine but, the problem arises when I start it from cmd.exe .

In this case it gives me this error:

System.ArgumentNull Exception: Value cannot be null. (Parameter 'connection String')

What's missing in this code?

The startup.cs is:

        public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));
        services.AddDbContext<ApplicationDataDbContext>(options =>
             options.UseSqlServer(
                Configuration.GetConnectionString("DATAconnection")));
        services.AddDatabaseDeveloperPageExceptionFilter();
        services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
            .AddEntityFrameworkStores<ApplicationDbContext>();
        services.AddRazorPages();
    }

The appsettings.json is:

    "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-CORE;Trusted_Connection=True;MultipleActiveResultSets=true",
    "DATAconnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-DATA;Trusted_Connection=True;MultipleActiveResultSets=true"

  }

来自应用程序控制台的错误屏幕截图

One possible reason may be that the DefaultConnection has a null value in some other configuration provider. ASP.Net has multiple configuration providers and they are checked in the following order (with the last one taking precedence over others). So, in this sequence, the appsettings.json comes the last to look for.

  1. appsettings.json file
  2. appsettings.{env.EnvironmentName}.json file
  3. local User Secrets
  4. Environment Variables
  5. Command Line Arguments

So if there is an environment variable or secret named DefaultConnection then that would take a precedence. I am guessing you have that defined somewhere in one of these providers.

First you need to check whether your environment is Development or other environment in launchsettings.json file.

在此处输入图像描述

If it is development then it will look for Connection string in appsettings.Development.json file and not in appsettings.json file.

Have you checked in your DbContext file that you are passing a valid constructor with options parameter like the following?

  public ApplicationContext(DbContextOptions options):base(options)
    {
        
    }

I've come to the conclusion that, there seems to be a "connectionString" bug, on Visual Studio or.Net Core.

This worked for me:

Startup.cs ->

        public void ConfigureServices(IServiceCollection services)
    {
        var connectionString = "Server=.\\SQLEXPRESS;Database=aspnet-CORE;Trusted_Connection=True;MultipleActiveResultSets=true";

        services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(connectionString));

        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));

        services.AddDatabaseDeveloperPageExceptionFilter();

        services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
            .AddEntityFrameworkStores<ApplicationDbContext>();
        services.AddRazorPages();
    }

Some explanations:

**a. To be covered on every situation i have left both the connection Strings on purpose.

b. This means that, if i call the "APP.exe" at system startup or though *.bat file, it will call the manually injected "connectionString".

c. Otherwise, if i double click on it, it will use the "DefaultConnection" from appsettings.json, as the default connection.**

Hope it helps someone save some headaches.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM