I am studying using code-based EF Migrations for a product that does not use EF. Everything works well, except that the command:
Add-Migration MyTestMigration
The following message is displayed:
An explicit migration cannot be created because the following explicit migrations are expected: [201206260845338_DannyTest]. Apply pending explicit migrations before attempting to generate a new explicit migration.
The reason for this is that the connection string is not known at build time, and EF accidentally created a database called "MyContextName". \ SQLExpress. I canβt apply the pending migration because it refers to database tables that are not in this database β we are just trying to use migrations as a way to execute our scripts;
So the questions are:
If we do not use automatic migrations (we have EnableAutomaticMigrations = false), why does Add-Migration require the database to be updated , although it absolutely did not affect the generated (empty) migration ? It's hard for me to believe that MS does not intend to use this use case when it works so hard; the only βbrokenβ thing is a check that does not affect any behavior.
Is there a way around this other than creating our own Add-Migration team, which simply duplicates what EF does, but skips the (apparently unnecessary) updated version of DB? I tried passing various arguments, but still could not get it to work.
Edit:
Actually, I have found a better way to solve this problem, but actually this is not the answer to these questions, so adding it here. I hope you have time to turn this into a message
The only reason I wanted to use Add-Migration was because of all the guffs that came with DbMigration; but I realized that with the base class, we could basically eliminate the need for this if the base class automatically generated a migration identifier from the attribute. The goal is identical for all our migrations, since the state of the model does not change. Now we just manually create our migrations like this (the date is required to create the identifier for EF to apply them in the correct order):
[Migration(2012, 6, 27, 12, 00, "Add new xxx fields for yyy")] internal class MyNewMigration : MyDbMigration { public override Up() {
The MyDbMigration class has the properties Target / Source / Id. The target is hard-coded (the same value as the Add-Migration created with the first migration), Source is null, and Id is some reflection that reads MigrationAttribute. This means that now we can simply create these classes manually; that is not much effort, now we do not need to worry about all the materials of IMigrationMetadata :-)
Danny Tuppeny Jun 26 '12 at 9:59 2012-06-26 09:59
source share