Reputation: 4240
I have an ASP.NET MVC application which uses ASP.NET Identity. I want to add a feature which resets the database. Here is the layout of the database used by Identity right now
I've created a controller that will contain the code to reset the database
public ActionResult GenerateDatabase()
{
//Drop all tables
//Readd all tables
return RedirectToAction("Register", "Account");
}
But I'm fairly new to working with SQL Server through C#. How can I reset the database through a C# controller (as in drop the tables and readd them)?
Upvotes: 1
Views: 1907
Reputation: 23200
You can do all you want via the Database
property of your DbContext
:
To delete the database just use:
yourDbContextInstance.Database.Delete();
To recreate the databse just use:
yourDbContextInstance.Database.Create(); // Also check CreateIfNotExists()
Upvotes: 1