EF ExecuteSqlCommand中的SqlException

时间:2014-10-10 14:37:36

标签: c# .net sql-server entity-framework

按照代码我生成的sql脚本使用EF:

 MyContext myContext = new MyContext();
  var configuration = new DbMigrationsConfiguration
  {
      AutomaticMigrationsEnabled = true,
      ContextType = typeof(MyContext),
      MigrationsAssembly = typeof(MyContext).Assembly
  };

   var migrator = new DbMigrator(configuration);

   var scriptor = new MigratorScriptingDecorator(migrator);
   string script = scriptor.ScriptUpdate(sourceMigration: null, targetMigration: null);

    myContext.Database.ExecuteSqlCommand(script);//************ SqlException *********

   Console.ReadKey();
  

错误:其他信息:“' GO'。

附近的语法错误      

'创建/更改程序'必须是查询批处理中的第一个语句。

     

' GO'

附近的语法不正确      

' GO'

附近的语法不正确      

')附近的语法错误。

[更新]

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
}
  

脚本:

CREATE TABLE [dbo].[Users] (
    [Id] [int] NOT NULL IDENTITY,
    [Name] [nvarchar](max),
    CONSTRAINT [PK_dbo.Users] PRIMARY KEY ([Id])
)
GO

CREATE PROCEDURE [dbo].[User_Insert]
    @Name [nvarchar](max)
AS
BEGIN
    INSERT [dbo].[Users]([Name])
    VALUES (@Name)

    DECLARE @Id int
    SELECT @Id = [Id]
    FROM [dbo].[Users]
    WHERE @@ROWCOUNT > 0 AND [Id] = scope_identity()

    SELECT t0.[Id]
    FROM [dbo].[Users] AS t0
    WHERE @@ROWCOUNT > 0 AND t0.[Id] = @Id
END
GO

CREATE PROCEDURE [dbo].[User_Update]
    @Id [int],
    @Name [nvarchar](max)
AS
BEGIN
    UPDATE [dbo].[Users]
    SET [Name] = @Name
    WHERE ([Id] = @Id)
END
GO

CREATE PROCEDURE [dbo].[User_Delete]
    @Id [int]
AS
BEGIN
    DELETE [dbo].[Users]
    WHERE ([Id] = @Id)
END
INSERT [dbo].[__MigrationHistory]([MigrationId], [ContextKey], [Model], [ProductVersion])
VALUES (N'201410101441244_AutomaticMigration', N'System.Data.Entity.Migrations.DbMigrationsConfiguration',  0x1F8B0800000...)

0 个答案:

没有答案