I'm trying to run some raw SQL against a LocalDb instance:
dbContext.Database.Migrate();
dbContext.Database.ExecuteSqlRaw(sql);
Where the sql
is read from this file:
/****** Object: StoredProcedure [dbo].[aspnet_AnyDataInTables] Script Date: 02/02/2021 22:11:41 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[aspnet_AnyDataInTables]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[aspnet_AnyDataInTables] AS'
END
GO
ALTER PROCEDURE [dbo].[aspnet_AnyDataInTables]
@TablesToCheck int
AS
BEGIN
-- Check Membership table if (@TablesToCheck & 1) is set
IF ((@TablesToCheck & 1) <> 0 AND
(EXISTS (SELECT name FROM sysobjects WHERE (name = N'vw_aspnet_MembershipUsers') AND (type = 'V'))))
BEGIN
IF (EXISTS(SELECT TOP 1 UserId FROM dbo.aspnet_Membership))
BEGIN
SELECT N'aspnet_Membership'
RETURN
END
END
[...abbreviated...]
All this script does is create the stored procs required by the SQL Membership provider. I generated the script from an existing database.
This script runs fine when run from SQL Server Management Studio against the same LocalDb instance but it throws a SqlException when run from code:
Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
Must declare the scalar variable "@TablesToCheck".
Incorrect syntax near ')'.
Must declare the scalar variable "@TablesToCheck".
Incorrect syntax near ')'.
Must declare the scalar variable "@TablesToCheck".
Incorrect syntax near ')'.
Must declare the scalar variable "@TablesToCheck".
Incorrect syntax near ')'.
Must declare the scalar variable "@TablesToCheck".
Incorrect syntax near ')'.
Must declare the scalar variable "@TablesToCheck".
Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
Must declare the scalar variable "@ApplicationId".
Must declare the scalar variable "@ApplicationId".
Must declare the scalar variable "@ApplicationId".
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
Must declare the scalar variable "@ApplicationId".
Must declare the scalar variable "@ApplicationId".
Must declare the scalar variable "@ApplicationId".
Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
Must declare the scalar variable "@Feature".
[...abbreviated...]
How do I get it to run without errors?
Using EF Core 3.1.3.