Entity Framework 4 - Update database schema from model. Without wiping the table data
I'm working on a new project where I have the luxury of working from a Model to Database approach using Entity Framework 4.
The project I'm working on is taking an agile approach where different phases will be rolled out over time.
Will the Model First approach work in my case? I noticed when you "Generate Datab开发者_StackOverflow社区ase from Model" it recreates the entire schema from scratch which will obviously wipe all the data that is in the db.
I was hoping for a more "Update Database from Model" approach where the db would just be altered to reflect the changes rather than recreated.
Does anyone have any experience working in this type of workflow with EF?
Thanks,
James Sheldon
you can now use migrations and commit what you desire on you database.
look at this article
You should follow the code first approach if the database is already created and you want tp apply the EF migrations when required.
if you want to control how the database is initialized, The Entity Framework has provided three options to initialize the database.
- Create database if not Exists
- Create Database always
- Drop and Create if Model changes
Create database if not Exists
This option will create the database if it does not exist. This is the default behaviour of Entity Framework Code first. If the model changes, then the application will throw an exception
public EFContext() : base("EFDatabase")
{
Database.SetInitializer<EFContext>(new CreateDatabaseIfNotExists<EFContext>());
}
精彩评论