Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Code First Migrations: MigrateDatabaseToLatestVersion without NUGET

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".

like image 482
Dennis Avatar asked Jul 23 '12 08:07

Dennis


1 Answers

Simply use MigrateDatabaseToLatestVersion.

when my application starts, it must create database, if database does not exists;

MigrateDatabaseToLatestVersion will do that

then database schema must be adjusted to the model;

MigrateDatabaseToLatestVersion will do that

if database was created just now, I want to create some indexes;

Create code based initial migration for your database and Sql method in Up method to define all indexes you need.

also, if database was created just now, it must be seeded by some initial data;

Again use Sql in initial migration or Seed method in migration configuration

all of these things must be performed automatically, without any NUGET commands or external tools

NuGet 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 use MigrateDatabaseToLatestVersion and you want to control migration from your code.

like image 118
Ladislav Mrnka Avatar answered Sep 28 '22 02:09

Ladislav Mrnka