Csharp 无法创建';';类型的';DbContext';.异常';无法解析类型';Microsoft.EntityFrameworkCore.DbContextOptions`1[Comm的服务
我在GraphQL(min 1.28.22)上关注一个视频,当我运行命令"DotNet ef Migrations Add AddPlatformToDb"时,它返回以下问题:
无法创建类型为‘’的‘DbContext’.try 激活‘CommanderGQL.Data.AppDbContext’时,出现异常‘Unable to解析’Microsoft.EntityFrameworkCore.DbContextOptions`1[CommanderGQL.Data.AppDbContext]‘类型的服务’.‘在try 创建实例时引发.有关设计时支持的不同模式,请参见https://go.microsoft.com/fwlink/?linkid=851728
尽管写了相同的东西,但它并没有在视频中造成任何问题.
CommanderGQL.csproj
:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="GraphQL.Server.Ui.Voyager" Version="7.7.1" />
<PackageReference Include="HotChocolate.AspNetCore" Version="13.9.0" />
<PackageReference Include="HotChocolate.Data.Entityframework" Version="13.9.0" />
<PackageReference Include="Microsoft.EntityframeworkCore.Design" Version="8.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityframeworkCore.SqlServer" Version="8.0.2" />
</ItemGroup>
</Project>
docker-compose.yaml
:
version: '3'
services:
sqlserver:
image: "mcr.microsoft.com/mssql/server:2017-latest"
environment:
ACCEPT_EULA: "Y"
SA_PASSWORD: "pa55w0rd!"
MSSQL_PID: "Express"
ports:
- "1433:1433"
appsettings.Development.json
:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"ConnectionStrings": {
"CommandConStr" : "Server=localhost,1433;Database=CommandsDB;User Id=sa;Password=pa55w0rd!"
}
}
appsettings.json
:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
Startup.cs
:
using CommanderGQL.Data;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace CommanderGQL
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
//public IConfiguration Configuration { get; }
private readonly IConfiguration Configuration;
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<AppDbContext>(opt => opt.UseSqlServer
(Configuration.GetConnectionString("CommandConStr")));
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// Configure production environment here
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
// Configure endpoints here
});
}
}
}
Platform.cs
:
using System.ComponentModel.DataAnnotations;
namespace CommanderGQL.Models
{
public class Platform
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string LicenseKey { get; set; }
}
}
AppDbContext.cs
:
using CommanderGQL.Models;
using Microsoft.EntityFrameworkCore;
namespace CommanderGQL.Data
{
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
public DbSet<Platform> Platforms { get; set; }
}
}
Program.cs
:
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/", () => "Hello World!");
app.Run();