Create an EF migration seed script using the Configuration class.

ef-migrations entity-framework

Question

I have EF migrations working nicely, but I also want to generate the sql script for the seed data from my DbMigrationsConfiguration class. The seed data runs ok when I do Update-Database, but when I do UpdateDatabase -Script I do not get the sql for the seed inserts. I tried -Verbose on a normal Update-Database but I do not see the seed statements output there either.

Is this possible?

1
26
5/3/2012 8:23:27 AM

Accepted Answer

No it is not possible. Configuration class is not part of migration itself - it is infrastructure executing the migration. You have single configuration class for all your migrations and its Seed method is executed after every migration run - you can even use context for seeding data and because of that this method is executed after the migration is completed = it cannot be part of migration. Only content of the migration class is scripted.

19
5/3/2012 8:23:18 AM

Popular Answer

Another solution/workaround is to have SSMS generate the script for you:

1) Start with a clean database generated by your DB initializer and seed method. Make sure the data you want scripted is in there.

2) Using SSMS, right-click the database, go to Tasks > "Generate Scripts...", and follow the wizard. Under Advanced options, be sure to select "Data only" for "Types of data to script".

3) From the generated script, copy required seed statements over to your target script.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow