EntityFramework6.Npgsql icon indicating copy to clipboard operation
EntityFramework6.Npgsql copied to clipboard

Error creating full database script after migrations run

Open roji opened this issue 9 years ago • 3 comments

From @jdcrutchley on February 20, 2015 21:42

This only occurs if I've already run migrations and specify I want to create a full database creation script like so:

Database-Update -Script -SourceMigration $InitialDatabase

The error generated is:

object reference not set to an instance of an object

If I specify any other source migration besides $IntialDatabase it works properly. Also, if I first revert all the way to zero then ask for script, it runs fine. So this is my current workaround:

Database-Update -TargetMigration $InitialDatabase
Database-Update -Script
Database-Update

So there's a fine workaround, but it would be nice to be able to generate the script without reverting first.

Copied from original issue: npgsql/npgsql#497

roji avatar May 12 '16 12:05 roji

From @peterz11 on September 25, 2015 21:29

I have confirmed this exact problem, using EF 6 and npgsql 2.2.7 for entity framework. By reverting to an empty database, this script will get generated. With migrations run, i also get the object null ref error. Here is more of the call stack:

System.NullReferenceException: Object reference not set to an instance of an object. at Npgsql.NpgsqlMigrationSqlGenerator.Convert(IEnumerable1 operations) at Npgsql.NpgsqlMigrationSqlGenerator.Convert(IEnumerable1 operations) at Npgsql.NpgsqlMigrationSqlGenerator.Generate(IEnumerable1 migrationOperations, String providerManifestToken) at System.Data.Entity.Migrations.DbMigrator.GenerateStatements(IList1 operations, String migrationId) at System.Data.Entity.Migrations.Infrastructure.MigratorBase.GenerateStatements(IList`1 operations, String migrationId) at System.Data.Entity.Migrations.Infrastructure.MigratorScriptingDecorator.ScriptUpdate(String sourceMigration, String targetMigration) at System.Data.Entity.Migrations.Design.ToolingFacade.ScriptUpdateRunner.Run() at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate) at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate) at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner) at System.Data.Entity.Migrations.Design.ToolingFacade.ScriptUpdate(String sourceMigration, String targetMigration, Boolean force) at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0() at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command) Object reference not set to an instance of an object.

roji avatar May 12 '16 12:05 roji

Made a fix for the error. Do you think we can make the logic similar to the one used for the sqlserver, i.e. get latest migration id and then wrap every migration into if blocks using anonymous blocks or something else so that such a script can update any version of the database?

xklonx avatar Oct 09 '20 18:10 xklonx

Any idea if the PR will be reviewed and merged anytime soon? We use EntityFramework6.Npgsql and we need to generate the same script (With IF statements) that's generated for SQL Server. IT would be really appreciated.

ghorbey avatar Feb 26 '21 14:02 ghorbey