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

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

    I have solved my issue by setting right base path. The problem is the migrations or anything else from different packages uses wrong path to the appsetting.json file. Not sure if it's an official issue.

    I have just changed my Startup.cs as follows:

    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, reloadOnChange: true)
                .AddEnvironmentVariables();
            Configuration = builder.Build();
        }
    

    After that you just need to copy your appsettings.json to the right place if it's missing there.

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

    I had a similar problem when I specified the ".UseContentRoot" as the current process path.

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseUrls("http://*:3001")
                .UseStartup<Startup>()
                .UseContentRoot(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName));
    

    thus when running Add-Migration the process path is different from the project bin path therefore the process can't find the appsettings.json file. when I removed the ".UseContentRoot" line the migration was successful

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

    Probably, the issue is with your DotNetCliToolReference from the csproj file. If you migrate the project from an older version of asp.net core, the DotNetCliToolReference is not automatically updated. Update the yourproject.csproj file to use the 2.0.0 version of the CLI as shown in the snippet bellow:

    <ItemGroup>
    
            ...
              <DotNetCliToolReference 
                   Include="Microsoft.EntityFrameworkCore.Tools.DotNet" 
                   Version="2.0.0" />
    </ItemGroup>
    

    Rerun, from the project folder, the dotnet command with -v switch to see results

    dotnet ef database update -v
    

    Also, recheck your Microsoft.EntityFrameworkCore nuget packages to reference the 2.0.0 version. Remove or update older EF packages. The minimum are:

    • Microsoft.EntityFrameworkCore.SqlServer

    • Microsoft.EntityFrameworkCore.Design

    both 2.0.0 at this moment.

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

    I'm stupid and I had typo

    {
      "Conn*ce*tionStrings": {
        "DefaultConnection": "Data source=***.db"
      },
    

    changed it to

    {
      "ConnectionStrings": {
        "DefaultConnection": "Data source=datingapp.db"
      },
    
    0 讨论(0)
提交回复
热议问题