Updating database schema with Entity Framework Code First

Adrian Grigore picture Adrian Grigore · Apr 14, 2011 · Viewed 19.2k times · Source

Entity Framework Code First is a great framework for developing new projects. But what about extending an existing database?

For instance what if I simply want to add an additional property to an existing data entity? Is there any "code first" way to do this or will I have to add a database column by hand using SQL Server Management Studio or a similar tool?

All I have found so far is how to regenerate the entire database from scratch when the schema changes, but I don't want to lose my data.

If I'm not mistaken, there is a tool for Ruby on Rails that can generate an SQL script reflecting the new database changes. Is there any way to do this in EF Code First as well?

Answer

parajao picture parajao · Jan 3, 2012

I'm currently using EntityFramework.Migrations library which addresses the problem of evolving the DB schema. It works creating migration classes which generate SQL scripts to update your schema.

It is still in beta at this time but I found it useful. Unfortunatly you should still generate data migration scripts.

You can find more information here.