【发布时间】:2014-11-28 06:32:54
【问题描述】:
使用 Entity Framework 6 和一个工作的 PostgreSQL 数据库加上连接,我正在尝试运行 Add-Migration 和 Update-Database 命令并通过控制台应用程序创建更新脚本。可以从控制台运行命令,因为它“只是 API 的薄包装”,请参阅 this SO-answer 了解我使用的示例。
migrations的创建工作,创建了三个常规文件,分别是:
Initial.cs
Initial.Designer.cs
Initial.resx
这是不适用于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);
}
这里的Configuration 类如下所示:
public class Configuration : DbMigrationsConfiguration<ZkContext>
{
public Configuration ()
{
AutomaticMigrationsEnabled = false;
SetSqlGenerator("Npgsql", new PostgreSqlMigrationSqlGenerator());
}
}
PostgreSqlMigrationSqlGenerator 类来自this GitHub repository。
当我尝试运行上面的更新数据库部分时,控制台应用程序在migrator.Update(); 上崩溃。这是一个例外:
找不到任何适合指定文化或中性文化的资源。确保“Zk.Migrations.Initial.resources”在编译时被正确嵌入或链接到程序集“Zk.Migrations”中,或者所有所需的附属程序集都是可加载的并且是完全签名的。
这是堆栈跟踪:
System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists (mustSucceedToKeepDatabase=
{System.Action}) System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists
(mustSucceedToKeepDatabase={System.Action}) 在
中的 System.Data.Entity.Migrations.DbMigrator.Update (targetMigration=(null)) System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update() in
Zk.Migrations.MigrationsTool.Main () 在 /home/erwin/zaaikalender/Zk.Migrations
/MigrationsTool.cs:78
我不知道如何提供正确的资源并使脚本正常工作。我应该先运行Enable-Migrations,如果是的话怎么办?我希望有人可以帮助我!谢谢。
【问题讨论】:
标签: c# entity-framework mono entity-framework-migrations resx