我以前使用过以下运行良好的脚本:
public const string application =
@"SET IDENTITY_INSERT Application ON
INSERT INTO Application(ApplicationId, Name, ModifiedDate) VALUES (1, 'x', getdate() );
INSERT INTO Application(ApplicationId, Name, ModifiedDate) VALUES (2, 'y', getdate() );
INSERT INTO Application(ApplicationId, Name, ModifiedDate) VALUES (3, 'z', getdate() );
SET IDENTITY_INSERT Application OFF
DBCC CHECKIDENT( 'Application', RESEED )";
现在我想执行 DDL,但我收到一条错误消息:
public const string dropCreate =
@"
GO
ALTER TABLE [dbo].[TestAccount] DROP CONSTRAINT [TestAccount_Application]
GO
DROP TABLE [dbo].[TestAccount]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[TestAccount](
[TestAccountId] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](35) NOT NULL,
[ApplicationId] [int] NOT NULL,
[RowVersion] [timestamp] NOT NULL,
[ModifiedDate] [datetime] NOT NULL,
PRIMARY KEY CLUSTERED
(
[TestAccountId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[TestAccount] WITH CHECK ADD CONSTRAINT [TestAccount_Application] FOREIGN KEY([ApplicationId])
REFERENCES [dbo].[Application] ([ApplicationId])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[TestAccount] CHECK CONSTRAINT [TestAccount_Application]
GO";
}
HResult=-2146232060
Message=Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
我可以从 C# 执行 DDL。我正在使用的代码如下所示:
context.Database.ExecuteSqlCommand(TestAccountSQL.dropCreate);