1

I use .Net 6 and when I want to run my first migration, I get this error:

Value cannot be null. (Parameter 'connectionString')

My appsettings.json:

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=Solardb;integrated security=SSPI"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },

  "AllowedHosts": "*"
}

And this is my Configuration method in program.cs:

builder.Services.AddDbContext<SolarDbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("ConnectionStrings")));
2
  • The connection string you're trying to get is called "DefaultConnection", not "ConnectionsStrings". Commented Dec 29, 2021 at 12:19
  • You seem to use C#. That language is very different from C. Commented Dec 29, 2021 at 12:29

2 Answers 2

7

You don't need to get the whole section. GetConnectionString does it already. You just need to put the name of connection string

builder.Services.AddDbContext<SolarDbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
Sign up to request clarification or add additional context in comments.

Comments

0

MY appsettings.json

  "ConnectionStrings": {
"DefaulConnection": "Data source=skinet.db"} 

I used a much simpler structure while solving the problem. startup.cs :

services.AddDbContext<StoreContext>(x => 
x.UseSqlite(_config.GetConnectionString("DefaultConnection")));

Writing the following instead of the above may solve the problem.

services.AddDbContext<StoreContext>(x =>
x.UseSqlite(("DefaultConnection")));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.