10

私はしばらくの間、プロジェクト (VS2012 Express の ASP.NET MVC) で Entity Framework (5.0) を使用しています。ただし、現在、移行を追加することはできません。

PM > Add-Migration -projectName MyProject.DAL TestMigration
Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.

これが手がかりになるかどうかはわかりませんが、「Unable to ...」というテキストが赤で表示されます。

自動移行を有効にしようとしましたが (保留中のモデルの変更をコードベースの移行に書き込もうとしているので意味がありません)、データベースで必要な移行が行われます。ただし、プロジェクトに移行がないため、これは私が望むものではありません。

データベースを削除して、データベースを再作成しようとしました。データベースは (以前の移行まで) 再作成されますが、その後 Add-Migration を使用しようとすると、「更新できません..」というエラーが表示されます。

編集

-force パラメータを試しましたが、違いはありませんでした。

構成クラスの内容 (以前の移行後に何も変更していません):

    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
    }

    protected override void Seed(Bekosense.DAL.Context.BekosenseContext context)
    {           
        context.Database.ExecuteSqlCommand(Properties.Resources.TriggerAlertMessageDrop);
        context.Database.ExecuteSqlCommand(Properties.Resources.TriggerAlertMessageCreate); 
        context.Database.ExecuteSqlCommand(Properties.Resources.TriggerAlertMessageSentDrop);
        context.Database.ExecuteSqlCommand(Properties.Resources.TriggerAlertMessageSentCreate); 
        context.Database.ExecuteSqlCommand(Properties.Resources.AddDbUsers);
    }

編集 2 DbContext で次の行をコメントアウトすると、追加移行を実行できることがわかりました。

//Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Configuration>());

上記の行をアクティブのままにして、構成ファイル内のすべてをコメントアウトしても、まだ機能しません。Database.SetInitializer 行がこの奇妙な動作を引き起こすのはなぜですか?

4

4 に答える 4

4

過去に問題を引き起こした自動移行は絶対に使用しないでください (データベースを移行するときは、最初から正しい方法で移行してください!)

この行は、global.asax にあるはずです。

Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Configuration>());

そして、あなたの DbContext ではありません!

上記が役に立たない場合のその他のヒント:

アプリケーションに複数のレイヤーがある可能性があります。

Add-Migration 000  -StartupProjectName "NameOfTheProjectInSolutionExplorer" -ConfigurationTypeName "MyConfiguration"  -ConnectionString "theconnectionstring;" -ConnectionProviderName "System.Data.SqlClient" -Verbose

上記は、多層アプリケーションに使用する Add-Migration コマンドです。

データベースの更新についても同じこと

Update-Database -ConfigurationTypeName "SlaveConfiguration" -StartupProjectName "FacturatieMVCv2.Data" -Verbose -script
于 2013-05-30T08:57:19.273 に答える