I have a few stored procedures that I'd like to be created during a migration step in Entity Framework Core.
I added a new migration, and in the Up() method I put this simple script:
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:
- 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 Entity Framework Core then generates this script:
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?
var sp. stackoverflow.com/questions/20715292/…?CREATE OR ALTER PROCEDURE.EXEC('CREATE OR ALTER ...')but there should probably be a better way