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

EF Core and SQL Functions

$
0
0

Hello,

I'm trying to update a SQL FUNCTION in my database via EF Core migration. 

IF NOT EXISTS(SELECT * FROM [dbo].[_EFMigrationsHistory] WHERE [MigrationId] = N'20201108022316_UpdateCombineEventrFunction11072020')
BEGIN

                                            /* Drop function if it already exists so we can recreate it */
                                            DROP FUNCTION if exists [dbo].[GetCombinedResources]                                
                                     
END;

GO

IF NOT EXISTS(SELECT * FROM [dbo].[_EFMigrationsHistory] WHERE [MigrationId] = N'20201108022316_UpdateCombineEventrFunction11072020')
BEGIN

                                            /* This will take the name of the Resource Type and use the Event id and Event event id to obtain a list of resources of the resource type and combine them together separated by a comma */
                                            CREATE FUNCTION [dbo].[GetCombinedResources]
                                            (@resourceTypeName VARCHAR(max),
                                            @EventId INT,
                                            @EventEventId INT)
                                            RETURNS VARCHAR(max)
                                            AS
                                            BEGIN													  

                                            Return (Select distinct dbo.EventResources.EventResourceId
                                                                             FROM            dbo.Events INNER JOIN
                                                                                                       dbo.Events ON dbo.Events.EventId = dbo.Events.EventId INNER JOIN
                                                                                                       dbo.EventResourceTypes INNER JOIN
                                                                                                       dbo.EventResources INNER JOIN
                                                                                                       dbo.EventResourceEvents ON dbo.EventResources.EventResourceId = dbo.EventResourceEvents.EventResourceId ON 
                                                                                                       dbo.EventResourceTypes.EventResourceTypeId = dbo.EventResources.EventResourceTypeId ON 
                                                                                                       dbo.Events.EventId = dbo.EventResourceEvents.EventId
    														                                           where dbo.Events.EventId = @EventEventId and dbo.EventResourceTypes.Name = @resourceTypeName and dbo.Events.EventId = @EventId
                                            FOR JSON AUTO)
                                     
END;

GO

IF NOT EXISTS(SELECT * FROM [dbo].[_EFMigrationsHistory] WHERE [MigrationId] = N'20201108022316_UpdateCombineEventrFunction11072020')
BEGIN
    INSERT INTO [dbo].[_EFMigrationsHistory] ([MigrationId], [ProductVersion])
    VALUES (N'20201108022316_UpdateCombineEventrFunction11072020', N'2.2.1-servicing-10028');
END;

GO

When I run `script-migration -i` to test it on my database, I see this error:

Msg 156, Level 15, State 1, Line 1295
Incorrect syntax near the keyword 'FUNCTION'.
Msg 137, Level 15, State 2, Line 1311
Must declare the scalar variable "@scheduleId".

Incorrect syntax: 'CREATE FUNCTION' must be the only statement in the batch.

Are FUNCTIONs not allow to be added within EF Core Migration?


Viewing all articles
Browse latest Browse all 1698

Trending Articles



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