0

Target Migration name is set to CreateNewProperty but, the migration getting executed is Base

I am trying to use Migrations in my project but when trying to update the database, only the base migration is applied.

I have a total of two migrations.

  1. The Base migration(initialy created)
  2. CreateNewProperty(Change includes adding a new property to one of the tables)

I see Enable-Migration mentioned in the Microsoft Documentation but it seems to have gone obsolete.

In addition to this, I also a mention of using the following in the Context class to enable latest migrations, but SetInitialier() method is no longer available:

Database.SetInitializer(new MigrateDatabaseToLatestVersion<DatabaseContext, EF6Console.Migrations.Configuration>());

When I try to remove all migrations and bring it to the initial state by executing:

update-databse 0 it fails with the following error: ALTER TABLE DROP COLUMN failed because column 'test' does not exist in table 'Item'.

which is basically the update done in the CreateNewproperty migration and this error is expected as this migration was never applied. Why is EF trying to downgrade a migration, when it did not apply it in the first place.

  • Try simply calling `Update-Database` without the migration name – theemee Dec 13 '22 at 12:00
  • Please provide a FULL and CLEAR explanation of the problem. What migrations have you got, which ones are already applied and which one(s) do you want applied. Generally speaking, you just use `update-database` to apply all your migrations. Any that are already applied will not be reapplied. You specify a particular migration when you want to roll back to that one, removing one or more that have already been applied after it. You cannot apply a later migration without applying earlier ones. – jmcilhinney Dec 13 '22 at 12:17
  • @theemee Both Update-Database and Update-Database with the migration name mentioned yield the same result. – Aruna Maurya Dec 13 '22 at 12:29
  • @jmcilhinney updated the question. Hope it helps! – Aruna Maurya Dec 13 '22 at 12:30
  • Added another scenario with update-databse 0 command. – Aruna Maurya Dec 13 '22 at 13:24

5 Answers5

0

Use below command to Migrate to a Specific Version (Including Downgrade)

update-database -targetmigration:MigrationName

More information visit https://learn.microsoft.com/en-us/ef/ef6/modeling/code-first/migrations/#migrate-to-a-specific-version-including-downgrade

adc 9
  • 21
  • 1
  • 5
  • A parameter cannot be found that matches parameter name 'targetmigration'.. No longer supported. – Aruna Maurya Dec 13 '22 at 12:32
  • Please refer this page https://stackoverflow.com/questions/40783055/entity-framework-core-update-database-specific-migration – adc 9 Dec 13 '22 at 12:35
  • update-database -migration "name" command worked, but the problem remains the same:( It is not applying the base migration instead of the name mentioned. – Aruna Maurya Dec 13 '22 at 12:39
0

Is it possible that your snapshot has become messed up somehow? You could try deleting that and then create a new migration (one that you could throw away) to rebuild it. Then try running update-database again.

  • Yes did that as well. I also uninstalled my EntityFramework Tools and re-installed it(although this doesnt make sense :P ) If it helps, My .Net version is 4.8 and EF is 3.6.1 – Aruna Maurya Dec 14 '22 at 03:09
0

In order to get rid of writing Migration and Update commands each time your data model changes, you can simply write two command line files as EF CLI and just click them to execute Migration and Update commands. These command files create the Migration name using the date and time of your machine. For the add migration file set a name such as 01-add_migration.cmd (with .cmd extension) with the following content:

For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c_%%a_%%b)
For /f "tokens=1-2 delims=/:" %%a in ("%TIME: =0%") do (set mytime=%%a%%b)
dotnet ef --startup-project ../Product.Api migrations add V%mydate%_%mytime% -o Persistence/Migrations -c ApplicationDbContext
pause

and for the update database set e file name such as 02-update_db.cmd with the content as bellow:

dotnet ef --startup-project ../Product.Api database update  -c ApplicationDbContext
pause

You can add other parameters of Migration and Update commands as well.

Majid Shahabfar
  • 4,010
  • 2
  • 28
  • 36
  • I don't think automating the process is the problem here. The problem is that the Update databse is not applying the latest migration(with or without the name of the migration) – Aruna Maurya Dec 14 '22 at 03:08
0

So in my opinion, if you have created lots of migrations and started changing up the classes during the creation of your app, you might have missed something, which happens to many developers.

If that is your case, you should try to delete the migrations history in the database and the tables that were created using the migrations.

The migrations history table is located in the tables folder of the database and looks like this:

enter image description here

Then delete the migrations folder that you have in your Project. Search for it in the solution explorer.

It should look something like this:

enter image description here

Next, do what you would do from the beginning.

Add-Migration [Name]
Update-Database
  • The entry only gets added to the database when the Migration is applied, and since none of the migrations are successfully applied, it is not in the table. But yes, I tried to delete the Migrations folder and try. I also tried to move the Base Migration to a different folder, just in case EF is only considering the first one in the folder, but that didn't work either. – Aruna Maurya Dec 14 '22 at 03:12
  • @ArunaMaurya Did you try to select one startup project? – Imesashvili_Irakli Dec 14 '22 at 09:20
  • Yes, I specified a project from the dropdown – Aruna Maurya Dec 15 '22 at 07:23
0

Okay, so this is what worked: I deleted the database completely and then ran the update-databse command. On running this, first the base migration was applied and then the second migration. And now when I create a third migration and apply it is working as expected!