'CREATE VIEW' must be the first statement in a query batch

后端 未结 5 698
逝去的感伤
逝去的感伤 2020-12-09 15:36

Basically its what the title says. This is my code.

USE Assignment2;
GO

/* Player View (2 marks)
    Create a view which shows the following details of all          


        
5条回答
  •  时光取名叫无心
    2020-12-09 16:30

    You may also run into this issue if trying to execute scripts from Entity Framework migrations. I think this is due to EF running those scripts in a transaction (as mentioned in another answer to this question). You can get round that with this type of syntax:

    IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[V_MovieActors]'))
    EXEC dbo.sp_executesql @statement = N'CREATE VIEW [dbo].[V_MovieActors]
    AS
    SELECT       NEWID() AS Id, dbo.Movie.Title, dbo.Movie.ReleaseDate, dbo.Actor.FirstName + '' '' + dbo.Actor.LastName AS Actor, dbo.Actor.DateOfBirth
    FROM            dbo.Actor INNER JOIN
                             dbo.Movie ON dbo.Actor.Id = dbo.Movie.Actor_Id
    '
    

    Which turns the whole thing into a single command for SQL to execute. That approach comes from this very useful article Using SQL Views With Entity Framework Code First by Morgan Kamoga.

提交回复
热议问题