英文:
Command 'update-database' is not working in .NET 6 Entity Framework for API
问题
命令 update-database
在 .NET 6 实体框架中的 API 中不起作用。控制台窗口显示错误消息:
> 未初始化 ConnectionString 属性
在我运行命令 update-database
后。命令 add-migration
正常工作。
appsettings.json
文件:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb; Database=MovieContext; Trusted_Connection=True; MultipleActiveResultSets=true"
}
}
Program.cs
文件:
using AspNetCoreWebApi6.Models;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// 将服务添加到容器中。
builder.Services.AddDbContext<MovieContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("MovieContext")));
builder.Services.AddControllers();
// 了解有关配置 Swagger/OpenAPI 的更多信息,请访问 https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// 配置 HTTP 请求管道。
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
英文:
Command update-database
is not working in .NET 6 entity framework for API. Console window displays an error
> The ConnectionString property has not been initialized
after I run the command update-database
. Command add-migration
is working fine.
appsettings.json
file:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb; Database=MovieContext; Trusted_Connection=True; MultipleActiveResultSets=true"
}
}
Program.cs
file:
using AspNetCoreWebApi6.Models;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddDbContext<MovieContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("MovieContext")));
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();
答案1
得分: 0
你正试图获取名为 "MovieContext" 的连接字符串,而不是 "DefaultConnection"。
英文:
You are trying to get a connection string named "MovieContext" instead of "DefaultConnection"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论