我试图在我的EF4.1 CodeFirst应用程序中生成数据库时运行Elmah Sql Server DDL。
要实现这一目标,请使用我的DbInitializer.Seed
方法:
protected override void Seed(MyJobLeadsDbContext context)
{
// Run Elmah scripts
context.Database.ExecuteSqlCommand(GetElmahDDLSql);
}
GetElmahDDLSql
只是一个字符串常量,包含来自http://code.google.com/p/elmah/source/browse/trunk/src/Elmah/SQLServer.sql的整个DDL
不幸的是,当这个运行时,我得到以下异常:
Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near 'GO'.
Incorrect syntax near the keyword 'ALTER'.
Incorrect syntax near the keyword 'ALTER'.
Incorrect syntax near 'GO'.
Incorrect syntax near the keyword 'SET'.
Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
Must declare the scalar variable "@ErrorId".
Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
Must declare the scalar variable "@TotalCount".
Must declare the scalar variable "@PageIndex".
Must declare the scalar variable "@TotalCount".
Must declare the scalar variable "@Application".
Must declare the scalar variable "@PageSize".
Must declare the scalar variable "@PageSize".
Must declare the scalar variable "@Application".
Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
Must declare the scalar variable "@ErrorId".
Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
知道如何通过EF4.1正确执行DDL吗?
答案 0 :(得分:5)
据我所知,您无法使用GO
。 GO
不是SQL命令,它是SSMS中SQLCMD,OSQL或Query编辑器等工具的特殊批处理控制命令。使用ADO.NET或EF时,必须将SQL脚本划分为命令并分别执行每个部分=两个GO之间的每个部分都是单独的命令,需要自己的ExecuteSqlCommand
。脚本中的全局变量可能存在一些问题。
另一种方法是使用SQL Server Management Objects to execute whole script。