IT:AD:EF/Codefirst/Migrations:HowTo:Set up Seeding
Summary
I've already touched on how Seeding changes in the Migrations Overview.
Process
When MigrateDatabaseToLatestVersion is used, Seeding of Reference data, etc. gets moved from the old-style DbInitializers (the ones that dropped/recreated the Db) to the Migrations Configuration.cs file (it has a Seed method).
One does not move it to the individual Migrations (that's would bit of a pain…) as I'd like to believe that all seeding requirements are figured out early, before Migration is put into place – but I know that this will never be complete right from the start. This means that Seeding will be sporadically spread through individual Migrations.
But note that in the Configuration.cs Seed method one it will be invoked each time there is a migration – ie several times. Therefore, one has to use context.People.AddOrUpdate(c) rather than simply context.People.Add(c)…this has implications…
Continually Updating Seeding
At first this feels weird…But it makes sense.
Seeding is run after all Migrations are applied. So the AddOrUpdates are happening in the latest shape. If they were already Added in the past, they were updated by a Migration. And if this is the first time they are being Inserted, then they need to be inserted in the current shape. Either way, we're talking about entities in the current shape.