How to re-create database for Entity Framework

Step-by-step guide

Recently I needed to install a .NET 8 Web App that uses Entity Framework, but did not have the the database. Here's how I re-created the accompanying SQL Server database from the files in the "Migrations" folder.

1: Ensure the connection string is correct.

The most important bit, make sure your appsettings.json (or sppsettings.development.json) contains the correct connection string for your SQL Server database, it should look something like this:

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=MyDatabase;Trusted_Connection=True;MultipleActiveResultSets=true"
  }
}

2: Update "ApplicationDbContext" configuration

Locate and ensure your "ApplicationDbContext" is correctly setup to use the connection string. This typically looks like this

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }
}

If Identity framework is being used the ApplicationDbContext may look like this

    public class ApplicationDbContext : IdentityDbContext<AppUser>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
        {

        }
    }

If you want to use the Migrations 

If there are Migrations in the Migrations folder then continue here, if you dont want to use the existing Migrations then jump to 5

3: Run Entity Framework Core commands

Open a terminal (Command prompt, Powershell or Terminal in VS Code), ensure you navigate to the location of the ".csproj" file location

4: Apply Migrations to the Database

Run the following command to apply the migrations and create the database with the initial schema

dotnet ef database update

This command will look for the migration files in the "Migrations" folder and apply them all to the database specified in your connection string.

If all went well you should now be able to see the new SQL Server database.

5: Dont want to use the Migrations

Remove the Migrations from the Migrations folder

Run the following command in powershell window (ensuring you are in the correct project)

dotnet ef migrations add InitialCreate

Then to apply the migration run the following

dotnet ef database update

If all went well you should now be able to see the new SQL Server database.

Additional steps (if necessary)

Ensure EF Core Tools are installed:

If you haven't already, you may need to install the EF Core Tools globally or locally to your project. Run the following command to install them globally:

dotnet tool install --global dotnet-ef

Target Specific Migrations:

If you need to target a specific migration, you can specify it in the "update" command

dotnet ef database update 20220215112438_InitialCreate

Verify Database Creation

  • Once the command completes, check your SQL Server instance to verify that the database and its tables have been created according to the schema defined in your migrations.

Troubleshooting

  • If you encounter issues, ensure that your project is building correctly and that there are no issues with the ApplicationDbContext or the migrations themselves.
  • Double-check that the Entity Framework Core tools are installed and correctly referenced in your project.
  • If you are missing data, check the program.cs or the app for any Seed type method and run the app in such a way that triggers the Seed function, for example you could add the following to program.cs
if (args.Length == 1 && args[0].ToLower() == "seeddata")
{
    await Seed.SeedUsersAndRolesAsync(app);
    await Seed.SeedData(app);
}

then run the following command to execute the Seeding of data...

dotnet run seeddata
  • NOTE: you can always delete all the tables in the database and delete the Migrations folder after backing it up somewhere and start over again until you get it right.