I need help to clarify how EF Code First Migrations works on production machine. I've some entity classes and DbContext-derived class to access entities. Now, I want to perform these several things:
- when my application starts, it must create database, if database does not exists;
- then database schema must be adjusted to the model;
- if database was created just now, I want to create some indexes;
- also, if database was created just now, it must be seeded by some initial data;
- all of these things must be performed automatically, without any NUGET commands or external tools.
I've read some articles about migrations, but they're focused mostly on NUGET usage or pure automatic database updates at run-time (via MigrateDatabaseToLatestVersion
). I know about DbMigration
class, but I can't understand, how to glue together MigrateDatabaseToLatestVersion
strategy and DbMigration
.
UPDATE.
In fact, I cannot use NUGET in the project and I need possibility to make a migration "by hand".
Simply use
MigrateDatabaseToLatestVersion
.MigrateDatabaseToLatestVersion
will do thatMigrateDatabaseToLatestVersion
will do thatCreate code based initial migration for your database and
Sql
method inUp
method to define all indexes you need.Again use
Sql
in initial migration orSeed
method in migration configurationNuGet and commands will help you to prepare it in design time (but you can simply reference required assemblies and write all code yourselves). Runtime doesn't need any powershell commands.
DbMigrator
is for scenarios where you don't want to useMigrateDatabaseToLatestVersion
and you want to control migration from your code.