How to stop add-migration validation when my database does not have pending migrations when using code-based migration?

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() { // ... } public override Down() { // ... } } 

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 :-)

+7
c # entity-framework ef-migrations
Jun 26 '12 at 9:59
source share
2 answers

Try commenting on existing migrations (those that were not applied to the database created on. \ SQLExpress), and restart the application. This should populate the local database with the necessary source tables.

Once the local database has the correct structure, you can uncomment your migrations and then use the update database to update the local one. Then you can add a new migration.

Also remember that the update-database command has the -connectionString parameter, so you can target a specific server / db.

+1
Jun 27 '12 at 6:17
source share

I saw this error until I removed the original automatically generated migration code that the Package Manager originally created from the Solution.

That would be 201206260845338_DannyTest.cs in the case of Danny.

0
Feb 17 '14 at 16:31
source share



All Articles