Code first migrations - how to display pending mod

2019-02-04 03:01发布

问题:

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?

回答1:

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



回答2:

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.