sql-server-2008 – SqlException:“GO”附近的语法错误

我无法使用context. Database.ExecutesqlCommand()通过DbContext发送sql语句.

我试图执行

CREATE TABLE Phones([Id] [uniqueidentifier] NOT NULL PRIMARY KEY,[Number] [int],[PhoneTypeId] [int])
GO
ALTER TABLE [dbo].[Phones] ADD  CONSTRAINT [DF_Phones_Id]  
    DEFAULT (newid()) FOR [Id]
GO

错误字符串失败

Incorrect Syntax near the keyword 'ALTER'.
Incorrect Syntax near 'GO'.

但是在SSMS中运行该确切的语句运行没有错误?有关通过DbContext的认约束需要解决的任何问题.我看到使用约束并且IsDbGenerated设置为true的人的问题.我不知道这将如何适用于这里.

解决方法

GO不是sql的一部分,所以不能用ExecutesqlCommand()执行.在使用Management Studio或命令行工具时,请考虑GO作为分批的方式.相反,只要删除GO语句,你应该没事.如果您遇到错误,因为您需要分批运行命令,那么只需为要运行的每个批次调用ExecutesqlCommand()一次.

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 'EastRiver' 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...