How to recreate a database in EF if my model changes?

I created a DbContext as follows:

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

This led to the creation of my database and the two tables above. Great

Then I decided to add another DbSet to the mix, and I got an error message:

the model supporting the "Party" context has changed since the database was created

I know the fixes for this using modelbuilder.IncludeMetadataInDatabase = false;andDatabase.SetInitializer<ClubmansGuideDB>(null);

1) What is the right way to add my new classes to the context and create them in the database?

2) In my futile attempts to solve this, I deleted the tables myself and tried to restart the application without any success. Then I deleted the entire database and re-started and did not re-create the database. How can I start from scratch - is there a cache somewhere?

+5
3

, :

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

EF 4.1 .

, , . , / ( ).

, , , :

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

(using, )

+15

Entity Framework Database Initializer, , First First Migrations

:

Database.SetInitializer<myDB>(new DropCreateDatabaseIfModelChanges<myDB>());
+3

Try putting this in your Global.asax.cs Application_Start () method.

Database.SetInitializer<DatabaseContext>(null);

In reset, the database from scratch when starting the application will create a class like this

//Where myDB is your context
public class EntityBase: DropCreateDatabaseAlways<myDB>
{

} 

Then in your Application_Start () method you can do this

Database.SetInitializer(new EntityBase());
0
source

All Articles