Code First Migrations Updates Data Structure From Code

by Jenni Konrad on Dec 02, 2011 |

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


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

General Feedback
Marketing and all content copyright © 2006-2016 C4Media Inc. hosted at Contegix, the best ISP we've ever worked with.
Privacy policy

We notice you're using an ad blocker

We understand why you use ad blockers. However to keep InfoQ free we need your support. InfoQ will not provide your data to third parties without individual opt-in consent. We only work with advertisers relevant to our readers. Please consider whitelisting us.