I have 2 questions:
1) How can I run Seed() method from the package-manager console without updating-database model?
2) Is there a way how to call Seed() method in the code?
Thx for any advice.
I have 2 questions:
1) How can I run Seed() method from the package-manager console without updating-database model?
2) Is there a way how to call Seed() method in the code?
Thx for any advice.
Answer question 1:
People would usually work around this by either:
reference: http://blog.oneunicorn.com/2013/05/28/database-initializer-and-migrations-seed-methods/
Add a new public method into the
Configuration
class. The new method only calls the protected methodSeed
:Then call the new method from eg. a unit test:
Answering your first question. Create a Migration by running add-migration SeedOnly
Clear out all Up() and Down() code generated if there was any pending changes
Then you can Target a Specific Migration by running update-database -TargetMigration SeedOnly in the Package Manager console
Answering Question #2: Extract all the code from the Seed() method to another class. Then call that from within the Seed() method from the Configuration class:
Then you can call it from anywhere:
This is not exactly what you are looking for, but however take a look: Running Entity Framework Migrations via command line prompt This may help you or someone to forget application based database migration, because you can easily make scripts to run automatically...
If you want to
Update-Database --Target-Migration xxx
and you are surprised as i was thatseed()
method has not been run, you can try togit stash
all your changes, generate database from previous version usingUpdate-Database
(to last revision which runsseed()
always) andgit stash apply
then.It is ugly workaround but it helped me.
Btw: don't forget to stage your changes before stashing