4

我试图在我的 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 吗?

4

1 回答 1

5

据我所知,您不能使用GO. GO不是 SQL 命令,它是用于 SSMS 中的 SQLCMD、OSQL 或查询编辑器等工具的特殊批处理控制命令。使用 ADO.NET 或 EF 时,您必须将 SQL 脚本划分为命令并分别执行每个部分 = 两个 GO 之间的每个部分都是单独的命令,需要自己的ExecuteSqlCommand. 脚本中的全局变量可能存在一些问题。

另一种方法是使用SQL Server 管理对象来执行整个脚本

于 2011-03-30T07:55:25.120 回答