Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About Code First Database Evolution (aka Migrations)

I watched a screencast from MSDN BLOG that talks about database migration.

Is there anyone who knows when can we use this feature? It looks it doesn't work in CTP5 yet.

By the way, is there any way to seed the initial data after I changed the schema code?

This is what am I doing right now, it wipes all the data every time I altered the model.

DbDatabase.SetInitializer<Context>(
    new DropCreateDatabaseIfModelChanges<Context>());
like image 775
user469652 Avatar asked Jan 16 '11 02:01

user469652


2 Answers

They most likely get this migration feature in the RTM version which is targeted for the first quarter of 2011.

To populate database with some initial data you can create your own database initializer and have it inherit from your desired strategy (right now we have 2 options) and then override Seed method inside it:

public class MyInitializer : DropCreateDatabaseIfModelChanges<MyContext>
{
    protected override void Seed(InheritanceMappingContext context)
    {        
        MyEntity entity = new MyEntity()
        {
            ...
        };
        context.MyEntities.Add(entity);
        context.SaveChanges();
    }
}
like image 68
Morteza Manavi Avatar answered Oct 12 '22 13:10

Morteza Manavi


Alpha 3 is out now. http://blogs.msdn.com/b/adonet/archive/2011/09/21/code-first-migrations-alpha-3-no-magic-walkthrough.aspx

like image 33
Chris Moschini Avatar answered Oct 12 '22 13:10

Chris Moschini