简体   繁体   中英

Value cannot be null. Parameter name: connectionString

So I have been going around stackoverflow and checking all the issues relating to my problem and everything checks out but I'm still getting the same error.

Value cannot be null. Parameter name: connectionString

Is the result when I run my add-migration "Initial Migration".

here is my code at 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?}");
        });
    }

and here is my connectionstring at appsettings.json

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

note that I have a LibraryData project (which is another project) and the class consist of a class called LibraryContext

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

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

I can't seem to find what am I doing wrong as I have done everything according to the materials and questions online! can anyone help me out on this?

I think you've made a typo in your appsettings.json ConnectionString should be ConnectionStrings

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM