Elmah DDL 在通过 context.Database.ExecuteSqlCommand (EF4.1) 运行时给出 sql 错误
我尝试在 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
不幸的是,当运行时我得到以下异常:
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?
I am attempting to run the Elmah Sql Server DDL upon database generation in my EF4.1 CodeFirst application.
To accomplish this, in my DbInitializer.Seed
Method I have:
protected override void Seed(MyJobLeadsDbContext context)
{
// Run Elmah scripts
context.Database.ExecuteSqlCommand(GetElmahDDLSql);
}
GetElmahDDLSql
is just a string constant that contains the entirety of the DDL from http://code.google.com/p/elmah/source/browse/trunk/src/Elmah/SQLServer.sql
Unfortunately, when this is run I get the following exception:
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'.
Any idea how to correctly execute the DDL via EF4.1?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,您不能使用
GO
。GO
不是 SQL 命令,它是 SQLCMD、OSQL 或 SSMS 中的查询编辑器等工具的特殊批处理控制命令。使用 ADO.NET 或 EF 时,您必须将 SQL 脚本划分为命令并分别执行每个部分 = 两个 GO 之间的每个部分都是单独的命令,需要自己的 ExecuteSqlCommand。脚本中的全局变量可能存在一些问题。另一种方法是使用 执行整个脚本的 SQL Server 管理对象。
As I know, you can't use
GO
.GO
is not SQL command it is special batch control command for tools like SQLCMD, OSQL or Query editor in SSMS. When using ADO.NET or EF you must divide your SQL script to commands and execut each part separately = each part between two GOs is separate command which needs its ownExecuteSqlCommand
. There can be some issues with global variables in the script.Another approach is using SQL Server Management Objects to execute whole script.