5

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?

2
  • 2
    entity framework issue isn't the best title I have ever seen. Commented Jan 9, 2012 at 18:26
  • What use is that comment, when so many good answers? Commented Jan 9, 2012 at 20:48

3 Answers 3

15

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)

Sign up to request clarification or add additional context in comments.

1 Comment

Database.SetInitializer(new DropCreateDatabaseIfModelChanges<MyDbContext>()); Database.Initialize(true); why not work form me??
3

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>());

Comments

0

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

Database.SetInitializer<DatabaseContext>(null);

To reset the database from scratch on app run make 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());

1 Comment

you can also override the Seed method and fill your database with test data. msdn.microsoft.com/en-us/library/gg679212(v=vs.103).aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.