2 つの質問があります。
1) データベース モデルを更新せずにパッケージ マネージャー コンソールから Seed() メソッドを実行するにはどうすればよいですか?
2) コードで Seed() メソッドを呼び出す方法はありますか?
アドバイスありがとうございます。
2 つの質問があります。
1) データベース モデルを更新せずにパッケージ マネージャー コンソールから Seed() メソッドを実行するにはどうすればよいですか?
2) コードで Seed() メソッドを呼び出す方法はありますか?
アドバイスありがとうございます。
最初の質問に答えます。add-migration SeedOnly を実行して移行を作成する
保留中の変更がある場合は、生成されたすべての Up() および Down() コードを消去します
public partial class SeedOnly : DbMigration
{
public override void Up()
{
}
public override void Down()
{
}
}
次に、パッケージ マネージャー コンソールで update-database -TargetMigration SeedOnly を実行して、特定の移行をターゲットにすることができます。
調査の結果、最終的にこの問題の回避策を見つけました。
1)Configuration
公開する:
public sealed class Configuration : DbMigrationsConfiguration<YourContextClassHere>
2) 以下のコードを任意の場所に追加します。最新の移行が実行され、データベースが更新されます。
Configuration configuration = new Configuration();
configuration.ContextType = typeof(YourContextClassHere);
var migrator = new DbMigrator(configuration);
//This will get the SQL script which will update the DB and write it to debug
var scriptor = new MigratorScriptingDecorator(migrator);
string script = scriptor.ScriptUpdate(sourceMigration: null, targetMigration: null).ToString();
Debug.Write(script);
//This will run the migration update script and will run Seed() method
migrator.Update();