How can I perform database migration using Entity Framework 4.2?

I'm just starting to dive into ASP.net MVC3. I come from a Django background. One of the things I love about Django, is the add-on called "South". It lets me modify my Models in code, and then I run a command, it figures out what has changed, and it updates the database accordingly.

If I add, remove, or rename a field with the "code-first" approach in EF4, what happens? Does it just add or remove the field, and that's it? What if I want to do something like add a new field, and then perhaps run a Linq-to-SQL query to populate the new field, and then remove the old field? And I want a record of this so that when I go to deploy the change on my production server, it will run those 3 commands in sequence.

Is there something like that? Or how do people tackle situations like this? (It is pretty common...)


Edit: Found some links.

  • https://softwareengineering.stackexchange.com/questions/76082/is-entity-framework-code-first-a-bit-meaningless-useless-in-production-and-what
  • http://blogs.msdn.com/b/efdesign/archive/2010/10/22/code-first-database-evolution-aka-migrations.aspx
  • Using EF4 Code First: How can I change the Model without losing data
  • Deploying database changes with EF 4.1
  • https://github.com/dradovic/MigSharp
  • http://blogs.msdn.com/b/adonet/archive/2011/09/21/code-first-migrations-alpha-3-with-magic-walkthrough-automatic-migrations.aspx (Woo!)

  • Here's an excellent tutorial from Microsoft MVP David Hayden:
    http://www.davidhayden.me/blog/asp.net-mvc-4-and-entity-framework-database-migrations

    Not sure about 4.2 - but I installed the latest EF (4.3.1) and it worked like a charm.

    Pretty impressive - and I'm a Rails guy :P


    What happens depends on your initialisations, i'll discuss the "standards" using the dot net ways.

    Take a quick-deep dive into:

    Database Initialization Strategy - Look at the top, here the "DropCreateDatabaseAlways" is used, so it will always drop the Database and recreate it (you have other possabilities). - It says how EF should react on database changes.

    Here are the possible Database Initializations

    If you want Migrations, there are 2 ways: - Magic - No-Magic

    What you should know, is that Migrations is actually 1 week old (EF 4.2), it has been implemented in EF 4.1, though not with full support (Different DB, ...), but it's improving.

    It depends on how much time you have, but i'm waiting to implement 4.2 and EF Migrations, i'll implement it in a "test-project" on the end of the week, see if everything goes well and respond to the Ado.Net Team Blog (see links on Magic - No-Magic). Although i don't think there will be any problems.

    Best of luck on your choice :)

    链接地址: http://www.djcxy.com/p/15980.html

    上一篇: 实体框架6模型首次迁移

    下一篇: 我如何使用Entity Framework 4.2执行数据库迁移?