Elmah DDL gives sql errors when starting context.Database.ExecuteSqlCommand (EF4.1)

I am trying to start the DDL of the Elmah Sql server when creating a database 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 all DDL from http://code.google.com/p/elmah/source/browse/trunk/src/Elmah/SQLServer.sql

Unfortunately, when this is done, 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 properly execute DDL through EF4.1?

+4
source share
1 answer

As I know, you cannot use GO . GO not a SQL command. This is a special package management team for tools such as SQLCMD, OSQL or the query editor in SSMS. When using ADO.NET or EF, you must split the SQL script into commands and execute each part separately = each part between two GOs is a separate command that requires its ExecuteSqlCommand . There may be some problems with global variables in the script.

Another approach uses SQL Server Management Objects to execute entire scripts .

+5
source

Source: https://habr.com/ru/post/1345796/


All Articles