37

How do you run a code first entity framework migration without changing anything in the datamodel which would cause a migration to be created? I just want to run the seed method again because I added things to it.

Jhayes2118
  • 842
  • 1
  • 8
  • 18

1 Answers1

69

If you just need to run Seed() again, and nothing has changed that would cause a new migration to be added, just call Update-Database again with no flags and it will say 'No pending migrations', and run Seed() again for you. Seed() is called every time the DB is updated, regardless if there are new migrations or not.

DrewJordan
  • 5,266
  • 1
  • 25
  • 39
  • This doesn't seem to work it's a Down() migration via `Update-Database -TargetMigration foo`. Do you know of how Seed() could be manually run in that case? – twm Mar 05 '16 at 19:27
  • @twm Just saw this: Feel free to make a separate question and I can explain more, but `Seed` doesn't get called during a down migration, only up. You can however grab a reference to the context inside of a migration and do whatever you want with it. – DrewJordan May 12 '16 at 12:19
  • it didn't work. I see the running Seed method, publish to azure and the table is still empty. – user1019042 Sep 18 '16 at 20:13
  • @user1019042 it's pretty hard to diagnose with just that information. You should create a new question. – DrewJordan Sep 19 '16 at 10:41
  • Seed() is only called when the migration is executed from the command line – Dave Feb 14 '17 at 11:08
  • Can you explain what you mean by "Seed() is called every time the DB is updated, regardless if there are new migrations or not" => specifically, how can the DB be updated without a new migration? – vbp13 Sep 17 '18 at 14:21
  • @vbp13 you can update data in the database by using the `Seed()` method. Like I said in the answer, if you call `Update-Database` and there are no pending migrations, it will still fire off `Seed()` – DrewJordan Oct 23 '18 at 20:36
  • Not sure what about EF Core, but in EF 4.1.3 `Seed` is called only when there are any model changes. Repeated `Update-Database` calls have no affection, but "Running Seed method" message is still shown. – Ryan Feb 05 '19 at 10:17