Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

EF Core: Issue with creating FUNCTION in Ef Core Migration

$
0
0

I can't seem to figure out why this error is occurring?Error SQL80001: Incorrect syntax: 'CREATE FUNCTION' must be the only statement in the batch. Miscellaneous Files 

IF NOT EXISTS(SELECT * FROM [DataTools].[_EFMigrationsHistory] WHERE [MigrationId] = N'20200718013559_CreateCombineResourcesFuncForScheduler07172020')
BEGIN
    drop function if exists [dbo].[GetCombinedResources]
END;

GO

IF NOT EXISTS(SELECT * FROM [DataTools].[_EFMigrationsHistory] WHERE [MigrationId] = N'20200718013559_CreateCombineResourcesFuncForScheduler07172020')
BEGIN

                            // ERROR IS RIGHT HERE
Create FUNCTION dbo.GetCombinedResources (@resourceTypeName VARCHAR(max), @scheduleId INT, @scheduleEventId INT) RETURNS VARCHAR(max) AS BEGIN declare @result varchar(500) set @result = ''

I have not checked it into VSTS yet. I see this error when I complete:

PM> script-migration -i

Here is my migration:

        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.Sql(@"drop function if exists [dbo].[GetCombinedResources]");

            migrationBuilder.Sql(@"
                        Create FUNCTION dbo.GetCombinedResources
                        (@resourceTypeName VARCHAR(max),
                        @scheduleId INT,
                        @scheduleEventId INT)
                        RETURNS VARCHAR(max)
                        AS
                        BEGIN													  
                        declare @result varchar(500)
                        set @result = ''

                        select @result = @result + query.Name + ', ' from
                        (
                        SELECT distinct Resources.Name   FROM 
Events INNER JOIN Schedules ON Events.ScheduleId = ScheduleId and ScheduleEventId = @scheduleEventId ) query if (@result is null or @result = '') BEGIN return null END return LEFT(@result, NULLIF(LEN(@result)-1,-1)) END "); }


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>