值不能为空。参数名称:connectionString

时间:2018-01-24 14:13:12

标签: asp.net asp.net-core-2.0

所以我一直在使用stackoverflow并检查与我的问题相关的所有问题,所有内容都检查出来但我仍然遇到同样的错误。

值不能为空。 参数名称:connectionString

是我运行添加迁移"初始迁移"。

的结果

这是我在Startup.cs的代码

public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    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.AddMvc();

        services.AddDbContext<LibraryContext>(options 
            => options.UseSqlServer(Configuration.GetConnectionString("LibraryConnection")));
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseBrowserLink();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }

这是我在appsettings.json的连接字符串

{
 "ConnectionString": {
   "LibraryConnection": "Server(localdb)\\MSSQLLocalDB;Database=Library_Dev;Trusted_Connection=True;MultipleActiveResultSets=true"
},
   "Logging": {
     "IncludeScopes": false,
     "LogLevel": {
       "Default": "Warning"
     }
   }
}

请注意,我有一个LibraryData项目(这是另一个项目),该类包含一个名为LibraryContext的类

public class : DbContext
{
    public LibraryContext(DbContextOptions options) : base(options) { }

    public DbSet<Patron> Patrons { get; set; }
}   

我似乎无法找到我做错的事情,因为我已经根据网上的材料和问题做了一切!任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:4)

我认为您在appsetting.json中输入了一个拼写错误ConnectionString应该是ConnectionStrings

&#13;
&#13;
{
 "ConnectionStrings": {
   "LibraryConnection": "Server(localdb)\\MSSQLLocalDB;Database=Library_Dev;Trusted_Connection=True;MultipleActiveResultSets=true"
},
   "Logging": {
     "IncludeScopes": false,
     "LogLevel": {
       "Default": "Warning"
     }
   }
}
&#13;
&#13;
&#13;