Search code examples
c#asp.net-coreentity-framework-coreef-code-first

Entity Framework Core Add Migration gives Value cannot be null. (Parameter 'connectionString')


I'm new to .NET Core and working through an Udemy class. I'm trying to enable migrations to do code-first but I keep getting an error

Value cannot be null. (Parameter 'connectionString')

I've done some researching and it seems as though most of the time it's misspelling of ConnectionStrings in the appsetttings.json file. However, I've looked all over mine and tried several copy/paste to make sure I have the correct spellings. Either it's something else or I've looked at this so much I'm overlooking something.

Appsettings.json

{
    "ConnectionStings": {
        "DbConnection": "Server=DAVIDPC\\SQLEXPRESS01;Database=dotnet-rpg;Trusted_Connection=true;MultipleActiveResultSets=true;"
    },
    "Logging": {
        "LogLevel": {
            "Default": "Information",
            "Microsoft": "Warning",
            "Microsoft.Hosting.Lifetime": "Information"
        }
    },
    "AllowedHosts": "*"
}

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.AddDbContext<DataContext>(x => x.UseSqlServer(Configuration.GetConnectionString("DbConnection")));
    services.AddControllers();
    services.AddAutoMapper(typeof(Startup));
    services.AddScoped<ICharacterService, CharacterService>();
}

Solution

  • In your example you misspelled ConnectionStrings

     {
            "ConnectionStrings": {
                "DbConnection": "Server=DAVIDPC\\SQLEXPRESS01;Database=dotnet-rpg;Trusted_Connection=true;MultipleActiveResultSets=true;"
            },
            "Logging": {
                "LogLevel": {
                    "Default": "Information",
                    "Microsoft": "Warning",
                    "Microsoft.Hosting.Lifetime": "Information"
                }
            },
            "AllowedHosts": "*"
        }