seventhball
seventhball

Reputation: 33

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

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "AuthConnectionString": "Server=.;Database=EmployeeLeaveDatabase;Trusted_Connection=True"
}

Here please check

builder.Services.AddDbContext<AuthDbContext>(options => options.UseSqlServer(
    builder.Configuration.GetConnectionString("AuthConnectionString")));
builder.Services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<AuthDbContext>();

I was trying to add the migration but don't know why i am getting this error System.ArgumentNullException: Value cannot be null. (Parameter 'connectionString') at Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty(String value, String parameterName) at Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseSqlServer(DbContextOptionsBuilder optionsBuilder, String connectionString, Action`1 sqlServerOptionsAction) at Program.<>c__DisplayClass0_0.<$>b__0(DbContextOptionsBuilder options) in C:\Users\Seventhball\source\repos\EmployeeLeaveManagement\EmployeeLeaveManagement\Program.cs:line 9

Upvotes: 0

Views: 1617

Answers (1)

B_Cbk
B_Cbk

Reputation: 73

If defined like this:

"AuthConnectionString": "Server=.;Database=EmployeeLeaveDatabase;Trusted_Connection=True"

use this:

builder.Configuration.GetValue<string>("AuthConnectionString")

if you define it like this:

"ConnectionStrings": { "AuthConnectionString": "Server=.;Database=EmployeeLeaveDatabase;Trusted_Connection=True;"}

use this: :

builder.Configuration.GetConnectionString("AuthConnectionString")

Upvotes: 1

Related Questions