Search code examples
.netasp.net-mvc-3entity-frameworkef-model-first

Update Database from Model (when model changed)?


I'm developing an application (for my education, not commercial) using MVC3 and Model First approach.

I created a model and generated the SQL CE 4 database from it.

Next, I may need to change my model - i.e. add a column to a table, which is not a primary or foreign key column.

I only see an option "Generate Database From Model", which will drop my database and create a new one.

Is there a way to "Update Database From Model", which will preserve the database and just add the new columns, populating them with some default values or nulls?

A similar question Update database from model - Entity Framework was asked last year and suggested that the solution may become available in the future - but did anything change since then?


Solution

  • There is a package that takes care of that. It's EntityFramework.Migrations in NuGet repository. There's a good read for that : http://www.hanselman.com/blog/EntityFrameworkCodeFirstMigrationsAlphaNuGetPackageOfTheWeek10.aspx

    Package itself is on : http://nuget.org/List/Packages/EntityFramework.Migrations

    It was also presented by Scott Hanselman on MIX 11 http://channel9.msdn.com/Events/MIX/MIX11/FRM02 (really awesome video showing usage of scaffolding, elmah, glimpse and more )