mpen
mpen

Reputation: 283273

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.

Upvotes: 4

Views: 6781

Answers (2)

PhillipKregg
PhillipKregg

Reputation: 9358

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

Upvotes: 2

NicoJuicy
NicoJuicy

Reputation: 3528

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 :)

Upvotes: 2

Related Questions