‘update-database’命令在.NET 6 Entity Framework中不起作用,用于API。

huangapple go评论112阅读模式
英文:

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 文件:

  1. {
  2. "Logging": {
  3. "LogLevel": {
  4. "Default": "Information",
  5. "Microsoft.AspNetCore": "Warning"
  6. }
  7. },
  8. "AllowedHosts": "*",
  9. "ConnectionStrings": {
  10. "DefaultConnection": "Server=(localdb)\\mssqllocaldb; Database=MovieContext; Trusted_Connection=True; MultipleActiveResultSets=true"
  11. }
  12. }

Program.cs 文件:

  1. using AspNetCoreWebApi6.Models;
  2. using Microsoft.EntityFrameworkCore;
  3. var builder = WebApplication.CreateBuilder(args);
  4. // 将服务添加到容器中。
  5. builder.Services.AddDbContext<MovieContext>(options =>
  6. options.UseSqlServer(builder.Configuration.GetConnectionString("MovieContext")));
  7. builder.Services.AddControllers();
  8. // 了解有关配置 Swagger/OpenAPI 的更多信息,请访问 https://aka.ms/aspnetcore/swashbuckle
  9. builder.Services.AddEndpointsApiExplorer();
  10. builder.Services.AddSwaggerGen();
  11. var app = builder.Build();
  12. // 配置 HTTP 请求管道。
  13. if (app.Environment.IsDevelopment())
  14. {
  15. app.UseSwagger();
  16. app.UseSwaggerUI();
  17. }
  18. app.UseHttpsRedirection();
  19. app.UseAuthorization();
  20. app.MapControllers();
  21. 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:

  1. {
  2. &quot;Logging&quot;: {
  3. &quot;LogLevel&quot;: {
  4. &quot;Default&quot;: &quot;Information&quot;,
  5. &quot;Microsoft.AspNetCore&quot;: &quot;Warning&quot;
  6. }
  7. },
  8. &quot;AllowedHosts&quot;: &quot;*&quot;,
  9. &quot;ConnectionStrings&quot;: {
  10. &quot;DefaultConnection&quot;: &quot;Server=(localdb)\\mssqllocaldb; Database=MovieContext; Trusted_Connection=True; MultipleActiveResultSets=true&quot;
  11. }
  12. }

Program.cs file:

  1. using AspNetCoreWebApi6.Models;
  2. using Microsoft.EntityFrameworkCore;
  3. var builder = WebApplication.CreateBuilder(args);
  4. // Add services to the container.
  5. builder.Services.AddDbContext&lt;MovieContext&gt;(options =&gt;
  6. options.UseSqlServer(builder.Configuration.GetConnectionString(&quot;MovieContext&quot;)));
  7. builder.Services.AddControllers();
  8. // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
  9. builder.Services.AddEndpointsApiExplorer();
  10. builder.Services.AddSwaggerGen();
  11. var app = builder.Build();
  12. // Configure the HTTP request pipeline.
  13. if (app.Environment.IsDevelopment())
  14. {
  15. app.UseSwagger();
  16. app.UseSwaggerUI();
  17. }
  18. app.UseHttpsRedirection();
  19. app.UseAuthorization();
  20. app.MapControllers();
  21. app.Run();

‘update-database’命令在.NET 6 Entity Framework中不起作用,用于API。

答案1

得分: 0

你正试图获取名为 "MovieContext" 的连接字符串,而不是 "DefaultConnection"。

英文:

You are trying to get a connection string named "MovieContext" instead of "DefaultConnection"

huangapple
  • 本文由 发表于 2023年5月28日 03:15:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76348615.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定