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

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

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:

{
  &quot;Logging&quot;: {
    &quot;LogLevel&quot;: {
      &quot;Default&quot;: &quot;Information&quot;,
      &quot;Microsoft.AspNetCore&quot;: &quot;Warning&quot;
    }
  },
    &quot;AllowedHosts&quot;: &quot;*&quot;,
    &quot;ConnectionStrings&quot;: {
        &quot;DefaultConnection&quot;: &quot;Server=(localdb)\\mssqllocaldb; Database=MovieContext; Trusted_Connection=True; MultipleActiveResultSets=true&quot;
    }
}

Program.cs file:

using AspNetCoreWebApi6.Models;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddDbContext&lt;MovieContext&gt;(options =&gt;
options.UseSqlServer(builder.Configuration.GetConnectionString(&quot;MovieContext&quot;)));

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();

‘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:

确定