【问题标题】:Scaffolding Minimal Api with a separate DbContext project throws error "Could not load information for ..."脚手架 Minimal Api 与单独的 DbContext 项目引发错误“无法加载...的信息”
【发布时间】:2022-10-05 04:20:41
【问题描述】:

我有一个解决方案名称BugDemo,由 2 个项目组成。这里是the github repo

  • 一个名为Data的类库。
  • 一个名为Api 引用Data 项目的Asp.Net Core Minimal Api。我将Api项目设置为启动项目。

我用用户密码在这两个项目之间分享secret.json

{
  "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

我尝试使用以下方法调用脚手架:

$env:codegen_trace=1 
dotnet-aspnet-codegenerator minimalapi --project api

我收到以下错误:

行:minimalapi --project api 跟踪:执行外部命令: dotnet msbuild C:\Projects\BugDemo\api\Api.csproj /t:EvaluateProjectInfoForCodeGeneration /p:OutputFile=C:\Users\amd\AppData\Local\Temp\wybiwf1d.d4d;CodeGenerationTargetLocation=C:\Users\amd.dotnet\tools.store\dotnet-aspnet-codegenerator\7.0.0-rc.1.22452 .2\dotnet-aspnet-codegenerator\7.0.0-rc.1.22452.2\tools\net7.0\any;配置=调试-恢复

构建项目...跟踪:执行外部命令:dotnet build C:\Projects\BugDemo\api\Api.csproj --configuration 调试 --framework net7.0

跟踪:执行外部命令:dotnet exec --runtimeconfig C:\Projects\BugDemo\api\bin\Debug\net7.0\Api.runtimeconfig.json --depsfile C:\Projects\BugDemo\api\bin\Debug\net7.0\Api.deps.json C:\Users\amd.nuget\packages\microsoft.visualstudio.web.codegeneration.design\7.0.0- rc.1.22452.2\lib\net7.0\dotnet-aspnet-codegenerator-design.dll --no-dispatch --port-number 62322 minimumapi --project api --dispatcher-version 7.0.0-rc.1.22452.2

跟踪:命令行:--no-dispatch --port-number 62322 minimumapi --project api --dispatcher-version 7.0.0-rc.1.22452.2 脚手架失败。无法加载项目的信息 ..\Data\Data.csproj 追踪:在 Microsoft.VisualStudio.Web.CodeGeneration.Utils.RoslynWorkspaceHelper.GetProjectReferenceInformation(IEnumerable1 projectReferenceStrings) at Microsoft.VisualStudio.Web.CodeGeneration.Utils.RoslynWorkspace..ctor(IProjectContext projectInformation, String configuration) at Microsoft.VisualStudio.Web.CodeGeneration.Design.CodeGenCommandExecutor.AddFrameworkServices(ServiceProvider serviceProvider, IProjectContext projectInformation) at Microsoft.VisualStudio.Web.CodeGeneration.Design.CodeGenCommandExecutor.Execute(Action1 simModeAction) 在 Microsoft.VisualStudio.Web.CodeGeneration.Design.Program.<>c__DisplayClass4_0.<b__0>d.MoveNext() 运行时间 00:00:12.60

项目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);
    }
}

问题

如何解决这个问题?

【问题讨论】:

  • 首先,从dotnet-aspnet-codegenerator 文档中,我们可以看到该命令没有minimalapi 选项,因此该命令将不起作用。其次,如您所说,Data项目是类库,它应该提供关联方法来进行CRUD操作,那么,在API应用程序中,我们只能添加Data项目引用并调用关联方法,而不是直接使用 dbcontext 访问数据库。所以,试着修改你的代码。
  • 我正在与同样的问题作斗争。我的项目是 .NET 6 Core MVC。我的数据在一个单独的类库中,作为项目引用。

标签: c# asp.net-core entity-framework-core minimal-apis asp.net-core-scaffolding


【解决方案1】:

我已经向项目社区提出了这个问题。在搭建脚手架期间降级 Microsoft.VisualStudio.Web.CodeGeneration.Design 库的版本存在问题。据我了解,讨论是如何结束的——他们将问题分配给了开发人员,并且将来会得到修复(正如我在下一个主要版本中看到的那样)。 您可以在github 关注它。

脚手架项目在脚手架过程中更新项目的库并在后台进行构建。并错误地降级了自己的项目版本。

我的解决方法是使用预配置的身份创建一个干净的新项目,搭建我需要的脚手架并将结果复制到我的项目中。 完美运行。

【讨论】:

    猜你喜欢
    • 2022-09-28
    • 2022-09-28
    • 2022-10-07
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多