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

后端 未结 22 1872
时光取名叫无心
时光取名叫无心 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:16

    I had the same problem and what it is the I had to make sure that the name of the connection matches:

          services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            services.AddDbContext<MyContext>(options => options.UseSqlServer(Configuration["ConnectionStrings:DefaultConnection"]));
    

    which ****ConnectionStrings:DefaultConnection*** it was where I had the whole problem. Make sure that is the same in Startup.cs and appsettings.json(appsettings.Development.json in Vs 2019)

    After I fixed this, everything was fine.

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

    I had a similar issue. I had a typo in my appsettings.json. Changing ConnectionsStrings to ConnectionStrings did it for me!

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

    This worked flawlessly for me:

    public IConfiguration Configuration;
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext.ApplicationDbContext>(options => 
                //options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
                options.UseSqlServer("Server=serverAddress; Database=dbName; user=username; password=pswd"));
    }
    

    The commented part is just as reference where to replace.

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

    I had had a similar issue because of the following reasons:

    • appsettings.json was not included in the project
    • I was running the project from the path which did not contain appsettings.json
    0 讨论(0)
  • 2020-12-13 10:24

    I had a similar problem after trying to use new created project for ASP.NET Core 2.0 Web Api. As far as I found, the cause of the problem was that application settings specified for development environment were not added. I fixed it by updating startup file to the following:

    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();
        this.Configuration = builder.Build();
    }
    

    In my case program class looks like the following:

    public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }
    
        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .Build();
    }
    
    0 讨论(0)
  • 2020-12-13 10:25

    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.

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