Blazor Server Entity Framework: Value cannot be null. (Parameter 'connectionString')

Viewed 130

I have created a blazor server appliaction and am trying to connect it to my mysql database. I have set up the connection strings and services inside of startup but when I am trying to add the inital migration Add-Migration init -Context ApplicationDbContext I get back the error:

Value cannot be null. (Parameter 'connectionString')

My startup looks like this:

        public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        string identityConnectionString = Configuration.GetConnectionString("IdentityConnection");
        string dbConnectionString = Configuration.GetConnectionString("DebateBuddyConnection");

        services.AddDbContext<ApplicationDbContext>(options =>
        {
            options.UseMySql(identityConnectionString, ServerVersion.AutoDetect(identityConnectionString));
            options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
        });

        services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
            .AddEntityFrameworkStores<ApplicationDbContext>();

        services.AddEntityFrameworkMySql().AddDbContext<BuddyDbContext>(options => 
        {
            options.UseMySql(dbConnectionString, ServerVersion.AutoDetect(dbConnectionString));
            options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
        });       

        services.AddRazorPages();
        services.AddServerSideBlazor();
        services.AddBlazoredModal();
        services.AddSingleton<AppData>();
        services.AddScoped<AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider<IdentityUser>>();
        services.AddScoped<ClipboardService>();
        services.AddScoped<SMTPSender>();
        services.AddDatabaseDeveloperPageExceptionFilter();
    }

And my appsettings.json:

{
"ConnectionStrings": {
    "IdentityConnection": "Server=ipaddress; Database=dbname; Uid=username; Pwd=password;Trusted_Connection=True;MultipleActiveResultSets=true",
    "DebateBuddyConnection": "Server=ipaddress; Database=dbname; Uid=username; Pwd=password;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

If i set breakpoints when I run the app, I can see that my two variables:

 string identityConnectionString = Configuration.GetConnectionString("IdentityConnection");
 string dbConnectionString = Configuration.GetConnectionString("DebateBuddyConnection");

both contain the connection strings from appsettings.json

I get the same error if i run Add-Migration init -Context ApplicationDbContext or Add-Migration init -Context BuddyDbContext

Because my database is a MySQL database, i'm using the pomelo.EntityFrameworkCore.MySql nuget package, v5.0.0.

edit:

I also get the same error if i put the strings in manually like:

 services.AddDbContext<ApplicationDbContext>(options =>
        {
            options.UseMySql("Server=ipaddress; Database=dbname; Uid=username; Pwd=password;Trusted_Connection=True;MultipleActiveResultSets=true", ServerVersion.AutoDetect("Server=ipaddress; Database=database; Uid=username; Pwd=password;Trusted_Connection=True;MultipleActiveResultSets=true"));
            options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
        });

or like

 string identityConnectionString = "Server=ipaddress; Database=dbname; Uid=username; Pwd=password;Trusted_Connection=True;MultipleActiveResultSets=true";
        
        services.AddDbContext<ApplicationDbContext>(options =>
        {
            options.UseMySql(identityConnectionString, ServerVersion.AutoDetect(identityConnectionString));
            options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
        });
0 Answers
Related