我创建了一个 blazor 服务器应用程序,并试图将它连接到我的 mysql 数据库。我已经在启动时设置了连接字符串和服务,但是当我尝试添加初始迁移 Add-Migration init -Context ApplicationDbContext
时,我得到了错误:
Value cannot be null. (Parameter 'connectionString')
我的启动看起来像这样:
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();
}
还有我的 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": "*"
}
如果我在运行应用程序时设置断点,我可以看到我的两个变量:
string identityConnectionString = Configuration.GetConnectionString("IdentityConnection");
string dbConnectionString = Configuration.GetConnectionString("DebateBuddyConnection");
两者都包含来自 appsettings.json 的连接字符串
如果我运行 Add-Migration init -Context ApplicationDbContext
或 Add-Migration init -Context BuddyDbContext
,我会得到同样的错误
因为我的数据库是 MySQL 数据库,所以我使用的是 pomelo.EntityFrameworkCore.MySql nuget 包,v5.0.0。
编辑:
如果我手动输入字符串,我也会得到同样的错误:
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);
});
或者喜欢
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);
});