Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lose EF Code First Migration when working on different TFS branches?

We are using TFS and have different branches for our Dev.

  1. in the branch A we made a migration to change a column size

  2. in the branch B we made a migration to add a new table. This branch doesn not know about the branch A modification !!

  3. both modification are merged to the main branch.

When I do an update database, it does the 2 migration but at the end tells me there is pending changes. If I do an Add-Migration, it creates the same as the 1st migration (in branch A).

Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration. You can use the Add-Migration command to write the pending model changes to a code-based migration.

Is it because something is missing in the content of the property Target de IMigrationMetadata of my last migration since it didn't know about the 1st one ?

Is it possible to handle migrations in different TFS branches?

like image 990
bouh Avatar asked Apr 16 '12 09:04

bouh


People also ask

How do you manage migrations in a project with multiple branches?

Is there a good way to manage Migrations in a project with multiple branches? One way would be to merge, then delete all migration-files created while the branches were separate, and then create one new migration file that holds all changes from the time the branch was created until it was merged back in.

How do I disable Code First migrations?

You need to go to Management Studio, open your database tables, go to System Tables folder and remove __MigrationHistory table that is located there (for EF6 and above, it's located directly under Tables ). This will disable Migrations for good.

How do you add-migration to multiple contexts?

Using multiple context types One way to create multiple migration sets is to use one DbContext type per provider. Specify the context type when adding new migrations. You don't need to specify the output directory for subsequent migrations since they are created as siblings to the last one.

How do I drop migration in EF core?

Delete the row corresponding to your migration that you want to unapply (Say "yes" to the warning, if prompted). Run "dotnet ef migrations remove" again in the command window in the directory that has the project. json file. Alternatively, run "Remove-Migration" command in the package manager console.


2 Answers

An EF migration step contains a metadata file, that has a signature of the model that is the result of the migration step. The problem when merging is that the signature of the migration done in branch B doesn't include the stuff done in the migration in branch A. As long as the migrations are in the branches, this is correct. When merging it becomes wrong.

To remedy it, you have to regenerate the meta-data of the latter migration with

add-migration MyMigrationName 

Running add-migration on an existing migration without the -force parameter will regenerate just the metadata.

I wrote an in depth walk-through of a merging scenario in the EF Migrations and a Merge Conflict post on my blog.

like image 154
Anders Abel Avatar answered Nov 10 '22 17:11

Anders Abel


As an addition to Anders Abel's answer and for those of you who are having the issue when trying to regenerate the last migrations metadata causing EF to create a separate migration with a 1 appended to it.

You must include the full date/time stamp.

E.g.

If the filename of your previous migration is 201701011322_MakeChangesToPotatoTable

Then you must include the fullname properly in the "Add-Migration" command.

I.E.

Add-Migration 201701011322_MakeChangesToPotatoTable 
like image 38
JARRRRG Avatar answered Nov 10 '22 16:11

JARRRRG