开发者

About Code First Database Evolution (aka Migrations)

开发者 https://www.devze.com 2023-02-05 09:16 出处:网络
I watched a screencast from开发者_Go百科 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.

I watched a screencast from开发者_Go百科 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>());


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();
    }
}


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


EF 4.1 Candidate Release has been issued in March and looks like this Migration feature is not yet included

0

精彩评论

暂无评论...
验证码 换一张
取 消