Entity Framework: reset migrations


The last week I have been deep diving into Entity Framework and Code-First. For an old-school database programmer who started the career in Oracle SQL plus, and continued to Microsoft SQL Server Management Studio the transmission has not been easy 🙂

The concept is similar, but the definition of the database is very close to the code. You define your table as classes (model) and set up the columns as properties and relationship in navigation properties. This is easy to understand, but the difficult part is to find all the tweaks to be able to set the proper multiplicity (1:1 and 1:m,) default values for columns, and last but not least audit triggers on columns like CreatedOn, CreatedBy, UpdatedOn and UpdatedBy. However, these topics are covered for my next few posts.

After experimenting the last week with Entity Framework Code-First, I suddenly had about 10-15 different migrations/Versions in my solution. That was not ideal and I wanted to find a way to reset the migration history and make the current version of my domain model as the initial migration for the project.

This can be achieved by the following commands in the “Package Manager Console” in Visual Studio 2012:

update-database -targetmigration:"0" -force -verbose
add-migration Initial
update-database

Now, I have a clean “Models” and “Migration” folder…Quite nice!

Advertisements

3 thoughts on “Entity Framework: reset migrations

  1. Thanks man, that was I need it, really I appreciate it, I was really desperate cause my migration wasn’t working at all, I was looking for a way to clean-cache or reseting entity/code-first/migrations/configurations stuff.

    Thanks a lot pal

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s