Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code first migrations - how to display pending model changes?

I'm using code first migrations. Is there a way to display pending model changes in package manager console before I scaffold a new migration?

like image 623
Andrzej Gis Avatar asked Aug 11 '13 00:08

Andrzej Gis


People also ask

How do you write a pending model changes to a code based migration?

You can use the Add-Migration command to write the pending model changes to a code-based migration. This error occurs when you have pending changes in your database migrations that have not been added yet. If you find this error staring you in the face during your programming adventures, simply type Add-Migration .


2 Answers

The accepted answer tells how to get the SQL for a already scaffolded model change before applying to the database.

The original question regarded model changes pre-scaffolding (i.e. changes in the model since the last "add-migration" before running the next "add-migration" ...)

To that answer i will just say: scaffold anyway, that gives you your preview. By that i mean, run "add-migration preview" or something similar, it will create the scaffolded migration with the model changes that you are interested in. Then just delete if from your solution ...

The point here is that there is no need to "preview" when actually "doing" can be quickly undone. Some might think deleting a scaffolded migration version from the migrations section of the solution would break something, but no it is very well supported.

You can even test scaffold, then create the sql script as Colin suggest in his answer, to get the full SQL. Still nothing has been done at this point, so delete the migration version if you'd like.

like image 121
TCC Avatar answered Oct 19 '22 00:10

TCC


There is no way that I know of to view pending changes in the model before scaffolding the migration, but I can't think of a reason not to scaffold the migration using Add-Migration so that the pending changes can be viewed in the migration file. There is no need to apply those changes to the database and the scaffolded migration can always be deleted.

Once the migration is scaffolded, if you use Update-Database -Script entity framework generates a SQL script rather than executing the changes directly.

You can get help on the EntityFramework in the package manager using get-help EntityFramework

And you can get help on the Update-Database command using the following:

get-help Update-Database

get-help Update-Database -detailed

get-help Update-Database -full

like image 33
Colin Avatar answered Oct 19 '22 00:10

Colin