Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recreate database in EF if my model changes?

I created a DbContext like so :

   public class myDB : DbContext
   {
     public DbSet<Party> Parties { get; set; }
     public DbSet<Booking> Bookings { get; set; }
   }

This generated my DataBase and the two tables above..Great

I then decided to add another DbSet into the mix & I got an error:

the model backing the 'Party' context has changed since the database was created

I'm aware of the fixes for this using modelbuilder.IncludeMetadataInDatabase = false; and Database.SetInitializer<ClubmansGuideDB>(null);

1) What's the correct way to add my new classes to the context and have them generated in the DataBase?

2) In my futile attempts to solve this myself I deleted the tables and tried to re-run the app with no success I then deleted the full database and re-run and it doesn't re-generate the DB. How can I start from scratch - is there some cache somewhere?

like image 708
LenPopLilly Avatar asked Jan 09 '12 18:01

LenPopLilly


People also ask

How do you refresh the model when the DB changes?

Double click on the . edmx file then right_click anywhere on the screen and choose "Update Modle From DB". In the new window go to the "Refresh" tab and choose the changed table/view and click Finish. Save this answer.

How does Entity Framework handle model changes?

During the development process, the domain models will be changing often. You can configure Entity Framework to delete (drop) and recreate your database every time there is a change to your schema. To do this, you need to create an initializer class in the same folder as your DbContext class.

How do I update my Entity Framework database first?

Right-click anywhere on the design surface, and select Update Model from Database. In the Update Wizard, select the Refresh tab and then select Tables > dbo > Student. Click Finish.

What is the purpose of DropCreateDatabaseAlways database initializer in Entity Framework?

DropCreateDatabaseAlways: As the name suggests, this initializer drops an existing database every time you run the application, irrespective of whether your model classes have changed or not.


2 Answers

I believe you're looking for:

Database.SetInitializer<ClubmansGuideDB>(new DropCreateDatabaseIfModelChanges<ClubmansGuideDB>());

As of EF 4.1 and above.

Note that this assumes you have permission to even drop your database. I do this locally for ease of development but disable it in staging/production (I do a manual Schema Compare and push my changes).

By the way, for testing, if you need to force recreate the database, you can do:

using (var context = new ClubmansGuideDB()) {
    context.Database.Initialize(force: true);
}

(using if you don't already have a reference to your DB)

like image 135
kamranicus Avatar answered Oct 04 '22 17:10

kamranicus


You can let the Entity Framework recreate the whole database by using a Database Initializer or if you want to migrate data you can look at the Code First Migrations

The following would recreate your database when the model changes:

Database.SetInitializer<myDB>(new DropCreateDatabaseIfModelChanges<myDB>());
like image 45
Wouter de Kort Avatar answered Oct 04 '22 19:10

Wouter de Kort