首页 > 解决方案 > dotnet-aspnet-codegenerator 脚手架控制器:找不到主键

问题描述

在 VS Code 中使用 netcoreapp3.1(3.1.6 库)

我已经构建了一个简单的 web api 项目,对我的数据库 ( dotnet ef dbcontext scaffold) 进行了反向工程,其中包含没有键的视图,这应该不再是问题了

我可以看到在onModelCreating代码中添加了以澄清没有密钥:

modelBuilder.Entity<VwAverageRuns>(entity =>
            {
                entity.HasNoKey();

当我去生成一个控制器时,问题就出现了:

dotnet aspnet-codegenerator controller -name VwAverageRunsController -async -api -m VwAverageRuns -dc dbContext -outDir Controllers

它错误:

试图找出模型和 DbContext 的 EntityFramework 元数据:
未找到“VwAverageRuns”主键。

在 Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.b__6_0()
在 Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
在 Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.Execute(String[] args)
在 Microsoft .VisualStudio.Web.CodeGeneration.CodeGenCommand.Execute(String[] args)

但是从我读过的所有内容来看,hasNoKey()里面onModelCreating应该意味着我不需要指定密钥。这里发生了什么?

标签: entity-frameworkasp.net-web-api2

解决方案


推荐阅读