开发者

Elmah DDL giving sql errors when run by context.Database.ExecuteSqlCommand (EF4.1)

开发者 https://www.devze.com 2023-02-20 11:39 出处:网络
I am attempting to run the Elmah Sql Server DDL upon database generation in my EF4.1 CodeFirst application.

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 开发者_如何学Go'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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消