Elmah DDL 在通过 context.Database.ExecuteSqlCommand (EF4.1) 运行时给出 sql 错误

发布于 2024-10-28 07:16:15 字数 1635 浏览 0 评论 0原文

我尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

箜明 2024-11-04 07:16:15

据我所知,您不能使用 GOGO 不是 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 own ExecuteSqlCommand. There can be some issues with global variables in the script.

Another approach is using SQL Server Management Objects to execute whole script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文