QBoard » Supporting Tech Stack » Cloud » How to re-create database for Entity Framework?

How to re-create database for Entity Framework?

  • I have got into a bad state with my ASP.Net MVC 5 project, using Code-First Entity Framework. I don't care about losing data, I just want to be able to start fresh, recreate the database and start using Code-First migrations.

    Currently I am in a state where every attempt to Update-Database results in an exception being thrown or getting an error message. Also the website can't access the database correctly. How can I wipe all migrations, re-create the database and start from scratch without having to create a new project? In other words, I want to keep my code but drop the database.

    Later I will also want to get the deployment database (SQL Server on Azure) in sync. Again, I don't mind dropping all the data - I just want to get it working.

    Please provide any how-to steps to get back to a clean state. Much appreciated.

      July 27, 2020 2:55 PM IST
    0
  • This worked for me:

    1. Delete database from SQL Server Object Explorer in Visual Studio. Right-click and select delete.
    2. Delete mdf and ldf files from file system - if they are still there.
    3. Rebuild Solution.
    4. Start Application - database will be re-created.
      September 8, 2021 12:31 PM IST
    0
  • I would like to add that Lin's answer is correct.

    If you improperly delete the MDF you will have to fix it. To fix the screwed up connections in the project to the MDF. Short answer; recreate and delete it properly.

    Create a new MDF and name it the same as the old MDF, put it in the same folder location. You can create a new project and create a new mdf. The mdf does not have to match your old tables, because were going to delete it. So create or copy an old one to the correct folder.
    Open it in server explorer [double click the mdf from solution explorer]
    Delete it in server explorer
    Delete it from solution explorer
    run update-database -force [Use force if necessary]
    Done, enjoy your new db

    UPDATE 11/12/14 - I use this all the time when I make a breaking db change. I found this is a great way to roll back your migrations to the original db:

    Puts the db back to original
    Run the normal migration to put it back to current

    Update-Database -TargetMigration:0 -force [This will destroy all tables and all data.]
    Update-Database -force [use force if necessary]
      August 30, 2021 1:30 PM IST
    0
  • Follow below steps:

    1) First go to Server Explorer in Visual Studio, check if the ".mdf" Data Connections for this project are connected, if so, right click and delete.

    2 )Go to Solution Explorer, click show All Files icon.

    3) Go to App_Data, right click and delete all ".mdf" files for this project.

    4) Delete Migrations folder by right click and delete.

    5) Go to SQL Server Management Studio, make sure the DB for this project is not there, otherwise delete it.

    6) Go to Package Manager Console in Visual Studio and type:

    1. Enable-Migrations -Force
    2. Add-Migration init
    3. Update-Database

    7) Run your application

    Note: In step 6 part 3, if you get an error "Cannot attach the file...", it is possibly because you didn't delete the database files completely in SQL Server.

      July 27, 2020 3:04 PM IST
    0