如何修正'值不能为空。参数名称:connectionString'

时间:2019-07-16 10:04:02

标签: c# asp.net-core

我最近回到了用.net core 1.1编写的旧项目,我想将其迁移到.net core 2.2。

我创建了一个新项目,复制了所有旧文件,更新了所有NuGet程序包,而所有这些我都试图运行该项目。当我运行它时,我得到了:

  

System.ArgumentNullException :'值不能为null。参数名称:connectionString'

这是我的appsettings.json

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=Magazin1;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  },
  "AdminAccount": {
    "Email": "admin@Magazin.com",
    "Password": "admin"
  },
  "UserAccount": {
    "Email": "user@Magazin.com",
    "Password": "11234"
  }
}

Startup.cs:

public class Startup
{
    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

        if (env.IsDevelopment())
        {
            // For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
            builder.AddUserSecrets("aspnet-Magazin.Web-b7b6c0c8-2794-41a1-ad6c-528772b97f8a");
        }

        builder.AddEnvironmentVariables();
        Configuration = builder.Build();
        HostingEnvironment = env;

        MapperConfiguration = new MapperConfiguration(cfg =>
        {
            cfg.AddProfile(new AutoMapperProfileConfiguration());
        });
    }

    public IConfigurationRoot Configuration { get; }
    public MapperConfiguration MapperConfiguration { get; set; }
    private IHostingEnvironment HostingEnvironment;

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddDbContext<ApplicationDbContext>(options =>
              options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); -->error here
    }
}

0 个答案:

没有答案