With Entity Framework 6 and a working PostgreSQL database plus connection I am trying to run the Add-Migration
and Update-Database
commands and create an update script via a console app. It is possible to run the commands from a console since it are "just thin wrapper around an API", see this SO-answer for the example I used.
The creation of migrations work, three regular files are created, namely:
Initial.cs
Initial.Designer.cs
Initial.resx
Here's the console app that does not work for Update-Database
.
public static void Main()
{
// Specify the name of the database migration
// Note: make sure to create a new name for each new migration and prefix with
const string MIGRATION_NAME = "Initial";
// END USER INPUT
// Get executing path from which the location of the Update_Scripts and new
// Migrations can be determined.
var executingPath = AppDomain.CurrentDomain.BaseDirectory;
// Write to database (PowerShell: Update-Database)
var config = new Configuration();
var migrator = new DbMigrator(config);
migrator.Update(); // <= HERE IT CRASHES!
// Now create the PostgreSQL update script.
var scriptor = new MigratorScriptingDecorator (migrator);
string script = scriptor.ScriptUpdate (sourceMigration: null, targetMigration: null);
var updateScriptPath = Regex.Replace (executingPath, "Zk.Migrations/.*",
"Zk/App_Data/Update_Scripts");
File.WriteAllText (updateScriptPath + MIGRATION_NAME + ".postgresql", script);
Console.WriteLine ("Update script {0} written to App_Data/Update_Scripts folder", MIGRATION_NAME);
}
Here the Configuration
class looks as follows:
public class Configuration : DbMigrationsConfiguration<ZkContext>
{
public Configuration ()
{
AutomaticMigrationsEnabled = false;
SetSqlGenerator("Npgsql", new PostgreSqlMigrationSqlGenerator());
}
}
Where PostgreSqlMigrationSqlGenerator
class comes from this GitHub repository.
When I try to run the Update-Database part above the console app crashes on migrator.Update();
. This is the exception:
Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "Zk.Migrations.Initial.resources" was correctly embedded or linked into assembly "Zk.Migrations" at compile time, or that all the satellite assemblies required are loadable and fully signed.
And here's the stack trace:
System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists (mustSucceedToKeepDatabase=
{System.Action}) in System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists
(mustSucceedToKeepDatabase={System.Action}) in
System.Data.Entity.Migrations.DbMigrator.Update (targetMigration=(null)) in
System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update () in
Zk.Migrations.MigrationsTool.Main () in /home/erwin/zaaikalender/Zk.Migrations
/MigrationsTool.cs:78
I don't know how to provide the right resources and get the script to work. Should I run Enable-Migrations
first and if so how? I hope someone can help me out! Thanks.
Two things that need to be done to get the console app working to add a code-first migrations, create a script and update the database:
.cs.
,.resx
and.Designer.cs
files that are created when adding a migration the same name as the migration scaffolding name without the migration'sMigrationId
preprended. Otherwise the files could not be found..resx
file and chose BuildAction "EmbeddedResource", otherwise the resources cannot be found.Full working script of the console app (which uses the PostgreSqlMigrationSqlGenerator):