【问题标题】:Elmah DDL giving sql errors when run by context.Database.ExecuteSqlCommand (EF4.1)Elmah DDL 在 context.Database.ExecuteSqlCommand (EF4.1) 运行时给出 sql 错误
【发布时间】:2011-03-29 22:28:44
【问题描述】:

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

【问题讨论】:

    标签: c# tsql entity-framework elmah entity-framework-4.1


    【解决方案1】:

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

    另一种方法是使用SQL Server Management Objects to execute whole script

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-02
      • 2022-08-23
      • 1970-01-01
      • 2020-04-27
      • 2019-07-04
      • 1970-01-01
      相关资源
      最近更新 更多