1

I'm learning to use EF Code First Migrations from https://msdn.microsoft.com/en-us/data/jj591621.aspx

Somewhere it said:

Code First Migrations has two primary commands that you are going to become familiar with.

Add-Migration will scaffold the next migration based on changes you have made to your model since the last migration was created.

Update-Database will apply any pending migrations to the database.

I don't understand what's Add-Migration doing exactly. To more precise, my problem is with:

since the last migration was created

In order to create a migration, it should pick two database structure to compare.

Obviously, one side is the current structure of models in the code. But what is the other side? The options are:

  1. Populating a database structure by unifying all migrations from initial to the last migration before this?
  2. Comparing it to a database which has the old structure?
Community
  • 1
  • 1
mehrandvd
  • 8,806
  • 12
  • 64
  • 111

1 Answers1

3

Check the code behind files of your migrations - they contain a lot of metadata, including a snapshot of the model from when it was created.

So, when you run Add-Migration the process is approximately this:

  1. Build a model based on your code
  2. Find the previous model from your last migration (if applicable)
  3. Compare the two models
  4. Generate a migration based on the difference

There's a useful article with some information and videos that cover this in more detail.

Charles Mager
  • 25,735
  • 2
  • 35
  • 45