0

I am a newbie in Codefirst and I do not understand how it works correctly. I created 3 migrations, migration 1..3 via "Add-Migration" command and issued the relative update with "Update Database".

I have a Configuration.cs file in my Migration directory and custom database initializer (I am working on MySQL) I created in order to seed initial data.

I do not know what happens behind the scenes in the production environment where I do not have any database yet.

Who is responsible to create and update the database? Are the migrations executed one after the other?

Can you suggest me how this process works in production and share useful links?

Regards, Roberto

Imran Rashid
  • 1,328
  • 14
  • 21
Roberto G.
  • 171
  • 5
  • 12
  • What do you mean that you don't have a db on the production environment? – ilans Jan 05 '15 at 13:37
  • Hi, I am starting from scratch in production, i have just deployed the package. I think behind the scenes when a context class is called the first time the migration process will be called, but in what way? – Roberto G. Jan 05 '15 at 13:51
  • You should call from your initializer the `CreateIfNotExists` function, or use an initializer which does exactly that, which I don't recommand - it made me a lot of trouble. – ilans Jan 05 '15 at 13:54
  • I think what you are trying to figure out is where/when does this initizalization happens - Where I know all things start is the Global.asax Application_Start function. – ilans Jan 05 '15 at 13:56
  • Hi, I call a Database.Create, my problem is to understand how migrations work in the successive steps: are migration 1 to 3 launched in sequence as distinct command to DB ? – Roberto G. Jan 05 '15 at 13:58
  • Yes. Assuming you are using `Automatic Migrations` - All migrations are called one after another, in the sequence in which they were created. Their `Up` function is called. Afterwards the `Seed` function on your Configuration class should fire. – ilans Jan 05 '15 at 14:00

1 Answers1

0

Assuming you are using Automatic Migrations - All migrations are called one after another, in the sequence in which they were created. Their Up function is called. Afterwards the Seed function on your Configuration class should fire.

Each migration has a sort of a Time-Stamp inside its file name, like this:

enter image description here

if you are creating lots of databases out of a single context class like I do, and you are afraid of not having control over migrations, you can use manual migrations. It is called DbMigrator.

See my/others answer here how to use it.

Community
  • 1
  • 1
ilans
  • 2,537
  • 1
  • 28
  • 29
  • Hi, thanks for your explanation, if I set up automatic migartion to false, what is the behavior on a non dev/local environment? – Roberto G. Jan 05 '15 at 14:03
  • I don't always use `Automatic Migrations`. In some DBs I use it and some don't. Meaning - nothing would happen if you don't use it... if you are creating lots of databases out of a single context class like I do, and you are afraid of not having control over migrations, you can use manual migrations. It is called `DbMigrator`. – ilans Jan 05 '15 at 14:04
  • See the link I've put in my answer - it shows the option of manual migrations - in order to have full control and not afraid of unwanted migrations and stuff that can happen when `Automatic Migrations` are on. – ilans Jan 05 '15 at 14:10
  • Ok thanks for the good explanation, I hve another nice question. I have found in the migration history table an entry called "xxx_xxx_InitialCreate", is this a kind of default migration associated to the initial model? – Roberto G. Jan 05 '15 at 14:15
  • @RobertoG. - I quote: "This migration was generated because we already had Code First create a database for us, before we enabled migrations." . This was taken from here: http://msdn.microsoft.com/en-us/data/jj591621.aspx You can also theoretically after some time, or when you have trouble, delete the migrations history table and files, and make `Add-Migrations`. Then the initial create would generate and hold a code for creating all the database you already have - but there would be only one file and only one row inside the migration-hostory table. – ilans Jan 05 '15 at 14:19
  • Thanks for your explanation, I think I will use migrations on developement but in a non automatic way, i prefer to use scripts in production. – Roberto G. Jan 05 '15 at 14:39
  • Great. Loved to help :) – ilans Jan 05 '15 at 14:40
  • Thank you very much. Now I understood how migrations works ;) – Roberto G. Jan 07 '15 at 11:46