背景:
@H_301_2@我们有一个使用ef-migrations的项目,其中包含长时间开发中创建的多个(read〜60)迁移.当然这些迁移也涉及到:
@H_301_2@> drop constraints1,2
>创建触发器 @H_301_2@当我们跑步时,都是独角兽和彩虹
> The variable name ‘@number’ has already been declared
> How can I override SQL scripts generated by MigratorScriptingDecorator
> Entity Framework Migrations: Including Go statement only in -Script output @H_301_2@全错误:
>创建触发器 @H_301_2@当我们跑步时,都是独角兽和彩虹
Update-Database@H_301_2@因为每个迁移作为单独的批次运行.但是使用这些迁移创建sql脚本时
Update-Database -Script@H_301_2@我们遇到了一些问题,如下所述: @H_301_2@问题1: @H_301_2@在多个迁移文件中删除多个约束时,EF生成的脚本往往会重新声明其用于删除的变量.这是因为它确保了同一迁移文件中的变量名称的唯一性,但是在更改文件时,会重置计数器,从而重叠名称. @H_301_2@问题二: @H_301_2@sql强制CREATE TRIGGER始终是批处理中的第一个语句.当脚本生成时,EF忽略sql(“CREATE TRIGGER …”)的内容;因此不特别对待它.因此,该语句可能出现在脚本文件的中间,并出错. @H_301_2@解决方案:(或者我们以为!) @H_301_2@这两个问题的常见/常识解决方案是在正确的位置插入开始/结束sql批处理.手动这样做会使我成为一个非常有钱的人,所以这不是一个有效的解决方案. @H_301_2@相反,我们使用了technique provided by @DavidSette.创建一个从sqlServerMigrationsqlGenerator继承的新的BatchsqlServerMigrationsqlGenerator,它有效地覆盖了dropColumnOperation和sqlOperation,然后强制围绕敏感的一个GO语句:
protected override void Generate (System.Data.Entity.Migrations.Model.DropColumnOperation dropColumnOperation) { base.Generate(dropColumnOperation); Statement("GO"); }@H_301_2@嘘嘘: @H_301_2@此解决方案中断运行Update-Database而不使用-Script标志,并显示以下错误:
System.Data.sqlClient.sqlException (0x80131904): Could not find stored procedure 'GO'.@H_301_2@这是由我们的定制生成器添加的.现在我不知道为什么,但应该有一个很好的理由EF不认识GO! @H_301_2@更多信息: @H_301_2@> Migrations: Duplicate @var0 variables in script that drops two constraints
> The variable name ‘@number’ has already been declared
> How can I override SQL scripts generated by MigratorScriptingDecorator
> Entity Framework Migrations: Including Go statement only in -Script output @H_301_2@全错误:
Applying code-based migration: 201205181406363_AddTriggerForOverlap. GO System.Data.sqlClient.sqlException (0x80131904): Could not find stored procedure 'GO'. at System.Data.sqlClient.sqlConnection.OnError(sqlException exception,Boolean breakConnection,Action`1 wrapCloseInAction) at System.Data.sqlClient.sqlInternalConnection.OnError(sqlException exception,Action`1 wrapCloseInAction) at System.Data.sqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj,Boolean callerHasConnectionLock,Boolean asyncClose) at System.Data.sqlClient.TdsParser.TryRun(RunBehavior runBehavior,sqlCommand cmdHandler,sqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj,Boolean& dataReady) at System.Data.sqlClient.sqlCommand.RunExecuteNonQueryTds(String methodName,Boolean async,Int32 timeout) at System.Data.sqlClient.sqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion,String methodName,Boolean sendToPipe,Int32 timeout,Boolean asyncWrite) at System.Data.sqlClient.sqlCommand.ExecuteNonQuery() at System.Data.Entity.Migrations.DbMigrator.Executesql(DbTransaction transaction,MigrationStatement migrationStatement) at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Executesql(DbTransaction transaction,MigrationStatement migrationStatement) at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable`1 migrationStatements) at System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable`1 migrationStatements) at System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId,XDocument targetModel,IEnumerable`1 operations,Boolean downgrading,Boolean auto) at System.Data.Entity.Migrations.DbMigrator.ApplyMigration(DbMigration migration,DbMigration lastMigration) at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ApplyMigration(DbMigration migration,DbMigration lastMigration) at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations,String targetMigrationId,String lastMigrationId) at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1 pendingMigrations,String lastMigrationId) at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration) at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore() at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run() ClientConnectionId:ac53af4b-1f9b-4849-a0da-9eb33b836caf Could not find stored procedure 'GO'.@H_301_2@所以基本上修复脚本打破了一个重要的命令.请帮我决定哪一个是两个罪恶中的较小者!