6

Using Entity Framework 6 using a model first setup with

  • No connection string specified.
  • Simple single class POCO for model.

Run application, database created. Woot!

Delete .mdf/.ldf in App_Data folder.

Run application, database connection error.

Why did it not recreate the database?

P.S. Tried doing How to re-create database for Entity Framework? but it doesn't work.

So how do I convince EF to recreate the database (and why is this dang hard?)?

Community
  • 1
  • 1
Mike Ward
  • 3,211
  • 1
  • 29
  • 42
  • Please add the full text of the error you get. – Leron Feb 24 '14 at 20:00
  • 2
    Have you tried either of these? Database.SetInitializer(new MigrateDatabaseToLatestVersion()); OR Database.SetInitializer(new CreateDatabaseIfNotExists()); – Tarzan Feb 24 '14 at 20:05
  • Did you set `disableDatabaseInitialization` to `false` in the context configuration? – 48klocs Feb 24 '14 at 20:08
  • 1
    So it seem you are not looking. From what you've wrote it seems that once the Database is created `Run application, database created. Woot!` you have never actually deleted it from the SQL Server. So what I would do is just go to SQL Server Management Studio and delete it myself or as `Tarzan` and `Jerry` suggested try one of the `Initializers`. `DropCreateDatabase` in my opinion is what you are looking for in this case. – Leron Feb 24 '14 at 20:19

2 Answers2

10

In your DbContext class constructor, you can use one of the following values to specify what to do when you run your app:

        Database.SetInitializer<MyContext>(new CreateDatabaseIfNotExists<MyContext>());
        Database.SetInitializer<MyContext>(new DropCreateDatabaseIfModelChanges<MyContext>());
        Database.SetInitializer<MyContext>(new DropCreateDatabaseAlways<MyContext>());

Try using one of these initializers in your DbContext class.

Jerry
  • 6,357
  • 8
  • 35
  • 50
10

You likely deleted the MDF file in Windows Explorer. SQL Server LocalDb doesn't know about it and the error is probably complaining about not being able to locate the MDF file.

To delete it properly, you can delete the database from SQL Server Management Studio or SQL Server Object Explorer in Visual Studio.

Alternatively, you could have selected "Show All Files" in Visual Studio Solution Explorer, located the MDF file in App_Data, right-clicked to delete it from within Solution Explorer itself. Visual Studio seems to do the right thing and let SQL Server know about this change.

Sam Hobbs
  • 2,594
  • 3
  • 21
  • 32
Anthony Chu
  • 37,170
  • 10
  • 81
  • 71
  • Yep, deleted it from Windows Explorer. +1 for deleting from VS. Two great answers. Can't decide which one to mark as the answer. – Mike Ward Feb 24 '14 at 21:15
  • OK, the nod goes to Anthony. Great tip about VS SQL Server Object Explorer. – Mike Ward Feb 24 '14 at 21:21
  • These are very different answers. Mark the one that fixed your problem. If it was the deletion issue here, you likely didn't have to add an initializer as EF by default uses the `CreateDatabaseIfNotExists` initializer. – Anthony Chu Feb 24 '14 at 21:22