分离DbContext项目的ASP.NET Core Minimal Api Scaffolding加载失败修复
修复Minimal API Scaffolding时无法加载Data项目的问题
问题背景
我有一个名为BugDemo的解决方案,包含两个项目:类库项目Data,以及引用Data项目的ASP.NET Core Minimal Api项目Api(已设为启动项目)。通过User Secret在两个项目间共享配置:
{"ConnectionStrings": {"DefaultConnection": "Server=.;Database=BugDemoDb;Integrated Security=true;TrustServerCertificate=true"}}
已通过以下命令成功生成数据库:
dotnet ef migrations add Initialization --startup-project Data --project Data dotnet ef database update --startup-project Data
但执行Scaffolding命令时:
$env:codegen_trace=1 dotnet-aspnet-codegenerator minimalapi --project api
出现核心报错:
Scaffolding failed. Could not load information for project ..\Data\Data.csproj
Api项目配置
<Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>net7.0</TargetFramework> <Nullable>enable</Nullable> <ImplicitUsings>enable</ImplicitUsings> <UserSecretsId>b3fdc987-781a-4fd4-853d-e279524cb5c6</UserSecretsId> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.0-rc.1.22427.2" /> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.0-rc.1.22426.7" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.0-rc.1.22426.7"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> </PackageReference> <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.0-rc.1.22452.2" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\Data\Data.csproj" /> </ItemGroup> </Project>
using Data; using Microsoft.EntityFrameworkCore; var builder = WebApplication.CreateBuilder(args); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); builder.Services.AddDbContext<AppDbContext>(opts => { opts.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")); }); var app = builder.Build(); if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseHttpsRedirection(); app.MapGet("/", async (AppDbContext ctx) => { return await ctx.Students.ToListAsync(); }); app.Run();
Data项目配置
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net7.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> <UserSecretsId>b3fdc987-781a-4fd4-853d-e279524cb5c6</UserSecretsId> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.0-rc.1.22426.7"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> </PackageReference> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.0-rc.1.22426.7" /> <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="7.0.0-rc.1.22426.10" /> <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.0-rc.1.22452.2" /> </ItemGroup> </Project>
namespace Data; public class Student { public int Id { get; set; } public string Name { get; set; } = default!; } using Microsoft.EntityFrameworkCore; namespace Data; public class AppDbContext : DbContext { public AppDbContext(DbContextOptions<AppDbContext> opts) : base(opts) { } public DbSet<Student> Students { get; set; } protected override void OnModelCreating(ModelBuilder mb) { base.OnModelCreating(mb); mb.Entity<Student>().HasData(new Student[] { new Student{ Id=1,Name="Albert Einstein"}, new Student{ Id=2,Name="Isaac Newton"}, new Student{ Id=3,Name="Blaise Pascal"}, new Student{ Id=4,Name="Nicola Tesla"} }); } } using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Design; using Microsoft.Extensions.Configuration; namespace Data; public class AppDesignTimeDbContextFactory : IDesignTimeDbContextFactory<AppDbContext> { public AppDbContext CreateDbContext(string[] args) { IConfiguration config = new ConfigurationBuilder() .AddUserSecrets<Data.AppDesignTimeDbContextFactory>() .Build(); var opts = new DbContextOptionsBuilder<AppDbContext>(); opts.UseSqlServer(config.GetConnectionString("DefaultConnection")); return new AppDbContext(opts.Options); } }
修复方案
1. 切换到解决方案根目录执行命令
代码生成工具需要识别整个解决方案的项目依赖,确保在解决方案根目录(C:\Projects\BugDemo)下执行命令:
cd C:\Projects\BugDemo $env:codegen_trace=1 dotnet-aspnet-codegenerator minimalapi --project Api
2. 显式指定项目参数
在命令中明确指定启动项目和数据项目的路径,帮助工具正确识别依赖:
$env:codegen_trace=1 dotnet-aspnet-codegenerator minimalapi --project Api --startup-project Api --data-project Data
3. 统一CodeGenerator包版本
当前使用的Microsoft.VisualStudio.Web.CodeGeneration.Design版本(7.0.0-rc.1.22452.2)与EF Core包版本不一致,将其调整为7.0.0-rc.1.22426.7,然后重新构建整个解决方案:
dotnet build BugDemo.sln
4. 验证项目引用路径
确认Api项目中<ProjectReference>的相对路径正确,确保Data项目文件在指定路径下存在,无损坏。
内容的提问来源于stack exchange,提问作者D G
相关产品推荐
相关产品推荐

