This preview is no longer current.
Code First Migrations is included in Entity Framework starting with Entity Framework 4.3.
We have released the first preview of our migrations story for Code First development; Code First Migrations August 2011 CTP. This release includes an early preview of the developer experience for incrementally evolving a database as your Code First model evolves over time.
Please be sure to read the ‘Issues & Limitations’ section of the announcement post before using migrations.
This post will provide an overview of the functionality that is available inside of Visual Studio for interacting with migrations. This post assumes you have a basic understanding of the Code First functionality that was included in EF 4.1, if you are not familiar with Code First then please complete the Code First Walkthrough.
public class Person { public int PersonId { get; set; } public string Name { get; set; } public string Email { get; set; } }.
What Changes Can Migrations Detect Automatically?
In this section we looked at adding a property, here is the full list of changes that migrations can take care of automatically:
So far we have looked at changes that migrations can infer without any additional information, now let’s take a look at renaming properties and classes.
public class Person { public int PersonId { get; set; } public string Name { get; set; } public string EmailAddress { get; set; } }
The renames parameter is a comma separated list of renames and can include class and property renames. Class renames use the same format as property renames i.e. –Renames:”Person=>Customer”.
Up until now we’ve let migrations take care of working out what SQL to execute. Now let’s take a look at how we can take control when we need to do something more complex.
Call for Feedback: From what we are seeing in our own internal use we don’t anticipate that custom scripts will be required very often. However, our efforts are somewhat sheltered from the ‘real world’ so we would love feedback on situations where you need to use custom scripts. In particular we are interested if there are significant scenarios where a code based alternative to writing raw SQL would be beneficial.
public class Person { public int PersonId { get; set; } public string EmailAddress { get; set; } public string FirstName { get; set; } public string LastName { get; set; } }
;
) NULL;
))
; GO
Run the ‘Update-Database’ command to bring the database up to date
With our custom script complete we can go back to using the automatic upgrade functionality, until we find the need to take control again. Migrations allows you to swap between automatic upgrade and custom scripts as needed. The Source.xml file associated with each custom script allows migrations to reproduce the same migration steps we have performed against other databases.
So far we have only made changes that avoid data loss but let’s take a look at how we can let an automatic migration execute even when it detects that data loss will occur.
public class Person { public int PersonId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } }
In this walkthrough we saw an overview of the functionality included in the first preview of Code First Migrations. We saw automatic migrations, including property and class renames as well as migrating with data loss. We also saw how to use custom scripts to take control of parts of the migration process. We really want your feedback on what we have so far so please try it out and let us know what you like and what needs improving.
Rowan Miller Program Manager ADO.NET Entity Framework