为什么我得到值不能为空。 (Parameter 'connectionString') 添加迁移后的错误消息
Why I get Value cannot be null. (Parameter 'connectionString') error message after Adding Migrations
我在尝试 Add-Migration
时收到错误消息,我收到错误消息
值不能为空。 (参数'connectionString')
到目前为止,我检查了 Startup.cs 文件并写了一些东西
using Microsoft.EntityFrameworkCore;
using ToDoS.Models;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<TodoContext>(opt =>
opt.UseSqlServer(connectionString));
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
所以我添加了这部分
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<TodoContext>(opt =>
opt.UseSqlServer(connectionString));
这是我的连接字符串
{
"Logging": {
"ConnectionStrings": {
"DefaultConnection": "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=ToDoApp;Integrated Security=True"
},
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
和 TodoContext
using Microsoft.EntityFrameworkCore;
namespace ToDoS.Models
{
public class TodoContext : DbContext
{
public TodoContext(DbContextOptions<TodoContext> options) : base(options) { }
public DbSet<Todo> Todos { get; set; }
}
}
添加迁移 InitCreate 后我收到错误消息。我在哪里弄错了?我在这里缺少什么?
连接字符串在 appsettings.json 的 Logging
元素中定义。它应该在根元素中定义:
{
"ConnectionStrings": {
"DefaultConnection": "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=ToDoApp;Integrated Security=True"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
或者,您应该可以使用 GetValue
:
获取字符串
var connectionString = builder.Configuration.GetValue<string>("Logging:ConnectionStrings:DefaultConnection");
我在尝试 Add-Migration
时收到错误消息,我收到错误消息
值不能为空。 (参数'connectionString')
到目前为止,我检查了 Startup.cs 文件并写了一些东西
using Microsoft.EntityFrameworkCore;
using ToDoS.Models;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<TodoContext>(opt =>
opt.UseSqlServer(connectionString));
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
所以我添加了这部分
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<TodoContext>(opt =>
opt.UseSqlServer(connectionString));
这是我的连接字符串
{
"Logging": {
"ConnectionStrings": {
"DefaultConnection": "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=ToDoApp;Integrated Security=True"
},
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
和 TodoContext
using Microsoft.EntityFrameworkCore;
namespace ToDoS.Models
{
public class TodoContext : DbContext
{
public TodoContext(DbContextOptions<TodoContext> options) : base(options) { }
public DbSet<Todo> Todos { get; set; }
}
}
添加迁移 InitCreate 后我收到错误消息。我在哪里弄错了?我在这里缺少什么?
连接字符串在 appsettings.json 的 Logging
元素中定义。它应该在根元素中定义:
{
"ConnectionStrings": {
"DefaultConnection": "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=ToDoApp;Integrated Security=True"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
或者,您应该可以使用 GetValue
:
var connectionString = builder.Configuration.GetValue<string>("Logging:ConnectionStrings:DefaultConnection");