.NET 8中使用EF Core+Identity认证遇迁移错误求助
问题:EF Core命名空间错误与迁移失败修复
背景
我正在学习.NET 8身份验证相关教程,执行dotnet ef migrations add InitialCreate命令时遇到问题,同时尝试集成OpenApi/Swagger。已安装的NuGet包如下:
- Microsoft.EntityFrameworkCore
- Microsoft.EntityFrameworkCore.Sqlite
- Microsoft.AspNetCore.Identity.EntityFrameworkCore
- Microsoft.AspNetCore.OpenApi
- Swashbuckle.AspNetCore
- Swashbuckle.AspNetCore.Annotations
代码片段
Program.cs
using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using RpgToolsApi.Models; using RpgToolsApi.Models.Auth; using System.Security.Claims; var builder = WebApplication.CreateBuilder(args); builder.Services.AddAuthentication().AddBearerToken(IdentityConstants.BearerScheme); builder.Services.AddAuthorizationBuilder(); builder.Services.AddDbContext<AppDbContext>(x => x.UseSqlite("DataSource=app.db")); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); builder.Services.AddIdentityCore<AppUser>() .AddEntityFrameworkStores<AppDbContext>() .AddApiEndpoints(); var app = builder.Build(); if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseHttpsRedirection(); app.MapIdentityApi<AppUser>(); app.MapGet("/", (ClaimsPrincipal user) => $"Hello {user.Identity!.Name}!") .RequireAuthorization(); app.MapPost("/roll", (int Dice, int Sides) => $"Rolling {Dice}d{Sides}...") .WithName("RollDice") .WithOpenApi(generatedOperation => { var numDiceParam = generatedOperation.Parameters[0]; numDiceParam.Description = "The number of dice to roll."; var numSidesParam = generatedOperation.Parameters[1]; numSidesParam.Description = "The number of sides on each die."; return generatedOperation; }); app.Run();
AppUser.cs(RpgToolsApi.Models.Auth命名空间)
using Microsoft.AspNetCore.Identity; namespace RpgToolsApi.Models.Auth { public class AppUser : IdentityUser {} }
AppDbContext.cs(RpgToolsApi.Models命名空间)
using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; using RpgToolsApi.Models.Auth; namespace RpgToolsApi.Models { public class AppDbContext : IdentityDbContext<AppUser> { public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) {} } }
错误信息
The type or namespace name 'EntityFrameworkCore' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)
该错误导致AddDbContext和AddEntityFrameworkStores调用报错,无法执行迁移创建数据库。
解决方案
安装EF Core命令行工具
执行以下命令全局安装EF Core工具:dotnet tool install --global dotnet-ef安装完成后验证版本:
dotnet ef --version补全EF Core Tools依赖包
向项目添加迁移工具必需的依赖包:dotnet add package Microsoft.EntityFrameworkCore.Tools --version 8.0.x替换
8.0.x为与其他EF Core包一致的版本。验证并修复NuGet包安装
- 打开项目的NuGet包管理器,确认
Microsoft.EntityFrameworkCore、Microsoft.EntityFrameworkCore.Sqlite、Microsoft.AspNetCore.Identity.EntityFrameworkCore均已正确安装且版本统一为8.x。 - 若包显示已安装但报错,先卸载再重新安装这些包。
- 打开项目的NuGet包管理器,确认
清理重建项目
执行命令清理项目缓存并重新构建:dotnet clean dotnet build指定项目执行迁移命令
若解决方案包含多个项目,需明确指定目标项目:dotnet ef migrations add InitialCreate --project RpgToolsApi或先进入项目所在目录,再执行迁移命令。
内容的提问来源于stack exchange,提问作者Dyndrilliac
相关产品推荐
相关产品推荐

