Entity Framework 核心添加迁移给出的值不能为空。 (参数'connectionString')
Entity Framework Core Add Migration gives Value cannot be null. (Parameter 'connectionString')
我是 .NET Core 的新手,正在使用 Udemy class。我正在尝试启用迁移以执行代码优先,但我一直收到错误
Value cannot be null. (Parameter 'connectionString')
我做了一些研究,似乎大部分时间都是 appsetttings.json
文件中 ConnectionStrings
的拼写错误。但是,我查看了我的所有内容并尝试了几次 copy/paste 以确保我的拼写正确。要么是其他原因,要么是我看了这么多我忽略了一些东西。
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>();
}
应用设置和配置服务方法看起来不错。
试试这个
"ConnectionStrings": {
"DbContext": "Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=dotnet-rpg;Integrated Security=SSPI; MultipleActiveResultSets=true;",
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
区别在于连接字符串没有"Data Source"
在配置服务中
services.AddDbContextPool<DataContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DbContext"))
);
如果这解决了您的问题,请告诉我。
下面两个参考 link 有实现细节。
Implement create, read, update, and delete functionalities using Entity Framework Core
在您的示例中,您拼错了 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": "*"
}
我是 .NET Core 的新手,正在使用 Udemy class。我正在尝试启用迁移以执行代码优先,但我一直收到错误
Value cannot be null. (Parameter 'connectionString')
我做了一些研究,似乎大部分时间都是 appsetttings.json
文件中 ConnectionStrings
的拼写错误。但是,我查看了我的所有内容并尝试了几次 copy/paste 以确保我的拼写正确。要么是其他原因,要么是我看了这么多我忽略了一些东西。
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>();
}
应用设置和配置服务方法看起来不错。 试试这个
"ConnectionStrings": {
"DbContext": "Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=dotnet-rpg;Integrated Security=SSPI; MultipleActiveResultSets=true;",
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
区别在于连接字符串没有"Data Source"
在配置服务中
services.AddDbContextPool<DataContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DbContext"))
);
如果这解决了您的问题,请告诉我。
下面两个参考 link 有实现细节。
Implement create, read, update, and delete functionalities using Entity Framework Core
在您的示例中,您拼错了 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": "*"
}