Code First Migrations Updates Data Structure From Code

| by Jenni Konrad Follow 0 Followers on Dec 02, 2011. Estimated reading time: 1 minute |

The ADO.NET Team has announced the release of Code First Migrations Beta 1 for Entity Framework (EF). The package is available from NuGet, under the name EntityFramework.Migrations.

The Code First approach to application development means that the object model is created in code, and then the database structure is generated from that model. It's similar to Model First development, which uses the EF Designer in Visual Studio to create the data model. (For a demonstration of Code First development in action, visit the ADO.NET Team's EF 4.2 Code First Walkthrough.)

Code First Migrations is a tool that incrementally updates the existing data structure based on changes in code. This can happen automatically or manually (Microsoft is conveniently referring to these as "with-magic" and "no magic" migrations), depending on the kind of updates that are being made to the model. The types of changes that can happen automatically are:

  • Adding a property or class
  • Renaming a property or class (some scripting is required for this to work properly)
  • Renaming a column or table without renaming its property or class
  • Removing a property

In any of these instances, running the command Update-Database will automatically apply changes to the data structure.

For all other updates, Code First Migrations uses the Add-Migration command to create a script of database changes. These migrations are all saved and time-stamped, to make it possible to roll back to previous versions of the data structure. In addition to structural changes to the database, migrations can include SQL commands for manipulating data. To commit a migration, it's just a matter of running Update-Database again. In order to copy one database to another, Code First Migrations can create a SQL script using Update-Database as well:

Update-Database –TargetDatabase:"NewDatabase" –Script

For more information, the ADO.Net team has walkthroughs of both the "no-magic" and "with-magic" methods of using Code First Migrations.

Microsoft states that this beta only includes the Visual Studio implementation of Code First Migrations, but that they plan to introduce command line and MSDeploy versions as well.

Rate this Article

Adoption Stage

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread


Login to InfoQ to interact with what matters most to you.

Recover your password...


Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.


More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.


Stay up-to-date

Set up your notifications and don't miss out on content that matters to you