c# - Blazor 服务器实体框架:值不能为空。(参数“连接字符串”)
问题描述
我创建了一个 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);
});
解决方案
推荐阅读
- html - 中心块或内联块(每行单个实例)元素没有包装但具有类似填充的间隙
- flutter - 如何为 DropDownMenu 添加另一个项目列表,然后使用小部件中的项目
- python - 具有开始和结束条件 Python 的序列的长度(计数)
- javascript - 如何使 forEach 循环同步
- windows - windows沙盒功能可以用于驱动测试吗?
- snowflake-cloud-data-platform - 执行 SQL 语句,出现在表中,并在 Snowflake 中返回结果记录集
- javascript - TypeError:无法读取未定义的属性“有”
- python - BeautifulSoup 4 - 在 div 之外抓取元素(h2)
- php - 是否可以暂时禁用从 PHP 运行所有 MySQL 查询?
- ruby-on-rails - 是什么导致'PG:undefinedColumn:ERROR:'在执行初始heroku rake db:migrate