Entity Framework 4 - Update database schema from model. Without wiping the table data
Asked Answered
S

2

29

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 Database 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

Straightout answered 29/6, 2010 at 19:41 Comment(4)
If you want more control over what changes you bring across, and do incremental and selective changes from CSDL->SSDL->database or database->SSDL->CSDL you may want to take a look at my 'Model Comparer' for EFv4: bit.ly/cCbnrnMisspell
There is now the Migrations support in the EF 4.3 release, which allows you to do incremental versioning of changes, data moves, and custom code per migration step.Spiritualty
Are you using EFCore or EF only. There is migration support as Dan mentioned. If it is a new proejct you are starting you should go with latest tech ie EF Core. More on Migrations - learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/…Uninspired
If you have the opportunity to start working on a higher version of EF or directly with EF Core will be much easier for you.Evermore
H
1

you can now use migrations and commit what you desire on you database.

look at this article

Hess answered 29/6, 2010 at 19:41 Comment(0)
P
0

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.

  1. Create database if not Exists
  2. Create Database always
  3. 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>());
}
Perfuse answered 3/8, 2022 at 12:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.