Search code examples
asp.net-mvcentity-frameworkasp.net-web-apiasp.net-core-1.0asp.net-core-2.0

Trouble with my connectionString. Unable to do migrations


Hi everyone, I am having some trouble with my connectionString. When I try to add migrations by typing

dotnet ef migrations InitialModel

I get the error below.

Value cannot be null.
Parameter name: connectionString   

This is my code on Startup.cs

public void ConfigureServices(IServiceCollection services)
            {
                services.AddDbContext<VegaDbContext>(options => 
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
                services.AddMvc();
            }

This is my code on appsettings.json

{
  "ConnectionStrings":{
    "Default": "server=localhost; database=vega; Integrated Security=SSPI"
  },
  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    "Console": {
      "LogLevel": {
        "Default": "Warning"
      }
    }
  }
}

This is my DbContext.

using Microsoft.EntityFrameworkCore;

namespace vega.Persistence
{
    public class VegaDbContext : DbContext
    {
        public VegaDbContext(DbContextOptions<VegaDbContext>options)
            :base(options)
        {

        }
    }
}

Solution

  • you have this in your configurationServices options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); but in your connectionstrings you have called it "Default": change one or the other to be the same.

    e.g.

    options.UseSqlServer(Configuration.GetConnectionString("Default")));