Ryan Langton
Ryan Langton

Reputation: 6160

EntityFramework 7 database already exists error on migrations

ASP.NET 5 project, Entity Framework 7. Using all the default stuff that comes with the ASP.NET 5 web template for creating the Identity Context. When I start up the app and first try to hit the context (register or log in), there is an error on Database.AsRelational().ApplyMigrations() that the database already exists. However, when I connect to the database matching the connection string in config.json, I do not see the database.

"ConnectionString": "Server=(localdb)\\mssqllocaldb;Database=aspnet-myAppDb;Trusted_Connection=True;MultipleActiveResultSets=true"

enter image description here

enter image description here

Upvotes: 2

Views: 2237

Answers (2)

Rob Prouse
Rob Prouse

Reputation: 22647

There is an already accepted answer, but I prefer doing this from Visual Studio. This is caused by manually deleting the MDB file. For future reference, you should not delete the MDB files from Explorer, but instead use Visual Studio or SQL Server Management Studio to delete them.

In Visual Studio, hit Ctrl+\, Ctrl+S to bring up SQL Server Object Explorer. If your keybindings are different, look under the View Menu.

Expand the node for the type of localdb you are using, find your problem database, right click and delete. Problem fixed.

Delete LocalDb

Upvotes: 3

Ryan Langton
Ryan Langton

Reputation: 6160

Connect to (localdb)\mssqllocaldb in SQL Server Management Studio, delete the database there. I'm not sure why this step is required or why the migrations fails, however.

Upvotes: 3

Related Questions