Adding a stored procedure using raw SQL and then generating an idempotent migration script results in flawed SQL script
- Dominant language
- C#
- Stars
- 14.8k
- Forks
- 3.4k
- PR merge metrics
- PR metrics pending
Description
I have a few stored procedures that I'd like to be created during a migration step in Entity Framework core 8.0.7.
I added a new migration, and in the `Up()` method I put this simple script:
```c#
var sp = @"CREATE PROCEDURE [dbo].[MyStoredProcedureFromMigration]
AS
BEGIN
SET NOCOUNT ON;
select * from Students where FirstName like '%dummy%'
END";
migrationBuilder.Sql(sp);
```
In my Azure Devops pipeline, I generate the idempotent migration script with a DotNetCoreCli task:
```yaml
- task: UseDotNet@2
inputs:
packageType: "sdk"
version: "8.0.x"
- task: DotNetCoreCLI@2
displayName: 'Install dotnet-ef tool'
inputs:
command: custom
custom: tool
arguments: 'update -g dotnet-ef'
- task: DotNetCoreCLI@2
displayName: 'Create EntityFramework migration script'
inputs:
command: custom
custom: ef
arguments: 'migrations script --idempotent --project $(Build.SourcesDirectory)/src/[MigrationsProject].csproj --startup-project $(Build.SourcesDirectory)/src/[MigrationsProject].csproj --output $(System.ArtifactsDirectory)/script.sql'
workingDirectory: $(Build.SourcesDirectory)
```
But EntityFramework then generates this script:
```sql
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20240827132758_NameOfMigration'
)
BEGIN
CREATE PROCEDURE [dbo].[MyStoredProcedureFromMigration]
AS
BEGIN
SET NOCOUNT ON;
select * from Students where FirstName like '%dummy%'
END
END;
GO
```
which returns an error when running from a SqlAzureDacpacDeployment task in the pipeline:
Incorrect syntax near the keyword 'PROCEDURE'.
If I remove the `--idempotent` argument, the script is valid but then I get errors because some of the tables already exist, as the generated script is no longer idempotent.
How can I generate a valid new migration script that runs without errors when it contains the creation of stored procedures?
Contributor guide
Assessment
This issue has not been assessed yet.