Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Entity Framework not recreate my localdb when deleted?

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?)?

like image 461
Mike Ward Avatar asked Feb 24 '14 19:02

Mike Ward


People also ask

How do I delete a LocalDB instance?

In order to be deleted, first it should be stopped. Type the SqlLocalDB stop MSSQLLocaDB command in the Command Prompt window: LocalDB instance “MSSQLLocalDB” stopped. Now, repeat the SqlLocalDB delete MSSQLLocalDB command.

How do I delete all records in Entity Framework?

Table select o; foreach (var row in rows) { dataDb. Table. Remove(row); } dataDb. SaveChanges();


1 Answers

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.

like image 168
Anthony Chu Avatar answered Sep 30 '22 18:09

Anthony Chu