Nick Gilbert Nick Gilbert - 1 month ago 14
ASP.NET (C#) Question

Reset ASP.NET Identity Database

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

enter image description here

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

Answer

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