簡體   English   中英

值不能為空。 參數名稱:connectionString

[英]Value cannot be null. Parameter name: connectionString

因此,我一直在研究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; }
}   

根據網上的資料和問題,我已經完成了所有工作,因此我似乎找不到我在做錯什么! 有人可以幫我嗎?

我認為您在appsettings.json中打了錯字ConnectionString應該是ConnectionStrings

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM