dotnet / dotnet/Scaffolding

"Primary key not found" when scaffolding heaps and views

Open
#1,579 0 comments 0 reactions 0 assignees View on GitHub
area-scaffolding customer-reported
Dominant language
C#
Stars
818
Forks
260
Avg merge
1d 8h
Merged PRs (30d)
10

Description

# Purpose

To add controllers for views and heaps via scaffolding in a web API project using Visual Studio or command line.

# Code Snippets

Create a default Web API project and SQL Server database with the scripts below.

## ASP.NET Core Web API Project Creation

### SaffoldPrimaryKeyIssue.sln
```
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31313.79
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ScaffoldPrimaryKeyIssue", "ScaffoldPrimaryKeyIssue\ScaffoldPrimaryKeyIssue.csproj", "{0F4B9484-FD0D-4251-934C-81F20CF24ADF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0F4B9484-FD0D-4251-934C-81F20CF24ADF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0F4B9484-FD0D-4251-934C-81F20CF24ADF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0F4B9484-FD0D-4251-934C-81F20CF24ADF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0F4B9484-FD0D-4251-934C-81F20CF24ADF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {49A8F219-249A-41BB-BF15-95C74C392269}
EndGlobalSection
EndGlobal
```

### ScaffoldPrimaryKeyIssue.csproj
```xml


net5.0





all
runtime; build; native; contentfiles; analyzers; buildtransitive



```

## SQL Server

### Database Creation

```tsql
USE [master]
GO

/****** Object: Database [ScaffoldTest] Script Date: 5/31/2021 2:19:16 PM ******/
CREATE DATABASE [ScaffoldTest]
CONTAINMENT = NONE
ON PRIMARY
( NAME = N'ScaffoldTest', FILENAME = N'D:\MSSQLSERVER\Data\ScaffoldTest.mdf' , SIZE = 8192KB , MAXSIZE = UNLIMITED, FILEGROWTH = 65536KB )
LOG ON
( NAME = N'ScaffoldTest_log', FILENAME = N'D:\MSSQLSERVER\Logs\ScaffoldTest_log.ldf' , SIZE = 8192KB , MAXSIZE = 2048GB , FILEGROWTH = 65536KB )
COLLATE SQL_Latin1_General_CP1_CI_AS
WITH CATALOG_COLLATION = DATABASE_DEFAULT
GO

IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [ScaffoldTest].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO

ALTER DATABASE [ScaffoldTest] SET ANSI_NULL_DEFAULT OFF
GO

ALTER DATABASE [ScaffoldTest] SET ANSI_NULLS OFF
GO

ALTER DATABASE [ScaffoldTest] SET ANSI_PADDING OFF
GO

ALTER DATABASE [ScaffoldTest] SET ANSI_WARNINGS OFF
GO

ALTER DATABASE [ScaffoldTest] SET ARITHABORT OFF
GO

ALTER DATABASE [ScaffoldTest] SET AUTO_CLOSE OFF
GO

ALTER DATABASE [ScaffoldTest] SET AUTO_SHRINK OFF
GO

ALTER DATABASE [ScaffoldTest] SET AUTO_UPDATE_STATISTICS ON
GO

ALTER DATABASE [ScaffoldTest] SET CURSOR_CLOSE_ON_COMMIT OFF
GO

ALTER DATABASE [ScaffoldTest] SET CURSOR_DEFAULT GLOBAL
GO

ALTER DATABASE [ScaffoldTest] SET CONCAT_NULL_YIELDS_NULL OFF
GO

ALTER DATABASE [ScaffoldTest] SET NUMERIC_ROUNDABORT OFF
GO

ALTER DATABASE [ScaffoldTest] SET QUOTED_IDENTIFIER OFF
GO

ALTER DATABASE [ScaffoldTest] SET RECURSIVE_TRIGGERS OFF
GO

ALTER DATABASE [ScaffoldTest] SET DISABLE_BROKER
GO

ALTER DATABASE [ScaffoldTest] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
GO

ALTER DATABASE [ScaffoldTest] SET DATE_CORRELATION_OPTIMIZATION OFF
GO

ALTER DATABASE [ScaffoldTest] SET TRUSTWORTHY OFF
GO

ALTER DATABASE [ScaffoldTest] SET ALLOW_SNAPSHOT_ISOLATION OFF
GO

ALTER DATABASE [ScaffoldTest] SET PARAMETERIZATION SIMPLE
GO

ALTER DATABASE [ScaffoldTest] SET READ_COMMITTED_SNAPSHOT OFF
GO

ALTER DATABASE [ScaffoldTest] SET HONOR_BROKER_PRIORITY OFF
GO

ALTER DATABASE [ScaffoldTest] SET RECOVERY SIMPLE
GO

ALTER DATABASE [ScaffoldTest] SET MULTI_USER
GO

ALTER DATABASE [ScaffoldTest] SET PAGE_VERIFY CHECKSUM
GO

ALTER DATABASE [ScaffoldTest] SET DB_CHAINING OFF
GO

ALTER DATABASE [ScaffoldTest] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF )
GO

ALTER DATABASE [ScaffoldTest] SET TARGET_RECOVERY_TIME = 60 SECONDS
GO

ALTER DATABASE [ScaffoldTest] SET DELAYED_DURABILITY = DISABLED
GO

ALTER DATABASE [ScaffoldTest] SET ACCELERATED_DATABASE_RECOVERY = OFF
GO

ALTER AUTHORIZATION ON DATABASE::[ScaffoldTest] TO [SANDMAN-HOME-PC\kento]
GO

ALTER DATABASE [ScaffoldTest] SET QUERY_STORE = OFF
GO

GRANT VIEW ANY COLUMN ENCRYPTION KEY DEFINITION TO [public] AS [dbo]
GO

GRANT VIEW ANY COLUMN MASTER KEY DEFINITION TO [public] AS [dbo]
GO

ALTER DATABASE [ScaffoldTest] SET READ_WRITE
GO
```

### Table Creation
```tsql
USE [ScaffoldTest]
GO

/****** Object: Table [dbo].[ClusteredExample] Script Date: 5/31/2021 2:21:04 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[ClusteredExample](
[Id] [INT] IDENTITY(1,1) NOT NULL,
[Username] [VARCHAR](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
CONSTRAINT [PK_ClusteredExample] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO

ALTER AUTHORIZATION ON [dbo].[ClusteredExample] TO SCHEMA OWNER
GO

/****** Object: Table [dbo].[HeapExample] Script Date: 5/31/2021 2:21:22 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[HeapExample](
[Id] [INT] NULL,
[Username] [VARCHAR](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
GO

ALTER AUTHORIZATION ON [dbo].[HeapExample] TO SCHEMA OWNER
GO
```

### View Creation
```tsql
USE [ScaffoldTest];
GO

/****** Object: View [dbo].[ClusteredView] Script Date: 5/31/2021 2:33:59 PM ******/
SET ANSI_NULLS ON;
GO

SET QUOTED_IDENTIFIER ON;
GO

CREATE VIEW [dbo].[ClusteredView]
AS
SELECT Id
, Username
FROM dbo.ClusteredExample;
GO

ALTER AUTHORIZATION ON [dbo].[ClusteredView] TO SCHEMA OWNER;
GO

/****** Object: View [dbo].[HeapView] Script Date: 5/31/2021 2:27:18 PM ******/
SET ANSI_NULLS ON;
GO

SET QUOTED_IDENTIFIER ON;
GO

CREATE VIEW [dbo].[HeapView]
AS
SELECT Id
, Username
FROM dbo.HeapExample;
GO

ALTER AUTHORIZATION ON [dbo].[HeapView] TO SCHEMA OWNER;
GO
```

### Insert Query
```tsql
INSERT INTO dbo.ClusteredExample (Username)
VALUES ('HelloWorld');

INSERT INTO dbo.HeapExample (Id, Username)
VALUES (1000, 'WorldHello');
```

## Visual Studio/Misc.

### Package Management Console (Create Models)

```powershell
Scaffold-DbContext 'Server=.;Database=ScaffoldTest;Integrated Security=SSPI;' Microsoft.EntityFrameworkCore.SqlServer -Output Models -ContextDir Context
```

### Powershell Commands (Using Global .exe To Create Controllers)

```powershell
# Works as expected (dbo.ClusteredExample) [Table]
dotnet-aspnet-codegenerator.exe -p "C:\Path\To\Project\ScaffoldPrimaryKeyIssue\ScaffoldPrimaryKeyIssue\ScaffoldPrimaryKeyIssue.csproj" controller -name ClusteredExampleController -api -m ScaffoldPrimaryKeyIssue.Models.ClusteredExample -dc ScaffoldTestContext -outDir Controllers -namespace ScaffoldPrimaryKeyIssue.Controllers

# Primary key not found (dbo.ClusteredView) [View]
dotnet-aspnet-codegenerator.exe -p "C:\Path\To\Project\ScaffoldPrimaryKeyIssue\ScaffoldPrimaryKeyIssue\ScaffoldPrimaryKeyIssue.csproj" controller -name ClusteredViewController -api -m ScaffoldPrimaryKeyIssue.Models.ClusteredView -dc ScaffoldTestContext -outDir Controllers -namespace ScaffoldPrimaryKeyIssue.Controllers

# Primary key not found (dbo.HeapExample) [Table]
dotnet-aspnet-codegenerator.exe -p "C:\Path\To\Project\ScaffoldPrimaryKeyIssue\ScaffoldPrimaryKeyIssue\ScaffoldPrimaryKeyIssue.csproj" controller -name HeapExampleController -api -m ScaffoldPrimaryKeyIssue.Models.HeapExample -dc ScaffoldTestContext -outDir Controllers -namespace ScaffoldPrimaryKeyIssue.Controllers

# Primary key not found (dbo.HeapView) [View]
dotnet-aspnet-codegenerator.exe -p "C:\Path\To\Project\ScaffoldPrimaryKeyIssue\ScaffoldPrimaryKeyIssue\ScaffoldPrimaryKeyIssue.csproj" controller -name HeapViewController -api -m ScaffoldPrimaryKeyIssue.Models.HeapView -dc ScaffoldTestContext -outDir Controllers -namespace ScaffoldPrimaryKeyIssue.Controllers
```

# Stack Trace For Failed Powershell Commands

```
Building project ...
Finding the generator 'controller'...
Running the generator 'controller'...
Attempting to compile the application in memory.
Attempting to figure out the EntityFramework metadata for the model and DbContext: 'ClusteredView'
dotnet-aspnet-codegenerator.exe : Primary key not found.
At line:1 char:1
+ dotnet-aspnet-codegenerator.exe -p "C:\Path\To\Project\Scaff ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Primary key not found.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError

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

# Provider and Version Info

Microsoft.VisualStudio.Web.CodeGeneration.Design/Microsoft.DotNet.MSIdentity version: 5.0.2
dotnet-aspet-codegenerator.exe version: 5.0.2
Target Framework: .NET 5.0
Database Engine: SQL Server Developer Edition (15.0.4123)
Operating System: Windows 10 Pro 2004 (Build 19041.985)
IDE: Visual Studio Community 2019 (16.9.6)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.