EF is not creating a MigrationsHistory table

David Thielen 2,401 Reputation points
2024-05-16T15:06:13.2033333+00:00

Hi all;

I've been using Entity Frameworks for some time with my app. And I just noticed, there is no MigrationsHistory table. I looked at the files in core/Migrations and none of them create a MigrationsHistory table.

Why is it missing and how do I get it?

And how do I populate it with the correct values for the 3 (so far) migrations I have created?

  • The SQL script script-migration created does try to insert a row into __EFMigrationsHistory. This fails as the table does not exist.

thanks - dave

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
708 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Hongrui Yu-MSFT 495 Reputation points Microsoft Vendor
    2024-05-17T07:34:58.2633333+00:00

    Hi,@David Thielen. Welcome to Microsoft Q&A. 

    1. To generate MigrationsHistory for the first time, you can use the following method Way 1: Use the following command in .NET Core CLI
    dotnet ef migrations add AddBlogCreatedTimestamp
    

    If you have multiple dbcontexts, you need to specify your dbcontext

    dotnet ef migrations add AddBlogCreatedTimestamp --context="MyDbContext1"
    

    Way 2: Use the following command in Visual Studio

    Add-Migration AddBlogCreatedTimestamp
    

    For more detailed information you can refer to the official documentation(Managing Migrations - EF Core | Microsoft Learn)

    1. If the generated MigrationsHistory does not match your expectations, you can customize your MigrationsHistory For more detailed information you can refer to the official documentation (Custom Migrations History Table)

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. AgaveJoe 26,426 Reputation points
    2024-05-20T13:32:14.0833333+00:00

    Is there any value in creating this table and inserting a row on every schema change?

    The __EFMigrationsHistory table has two columns MigrationId and ProductVersion. The MigrationId is the name of the migration prepended with a date string. If you are using PMC to add a migration then the command looks like.

    add-migration "MyMigration" 
    

    The result is a file named, 20240520173327_MyMigration

    The ProductVersion keeps tack of the Core version when the migration was Executed.

    Why is it missing and how do I get it?

    Frankly, I have no idea how you managed to execute a migrations without an __EFMigrationsHistory table. Maybe you deleted the table or you are looking in the wrong database.

    If you need to recreate the table then you could insert the migration file names in the MigrationId field and the Core version in the ProductVersion column.

    If you are still unsure, then create a new code first project to get an idea of what happens.

    https://learn.microsoft.com/en-us/ef/core/get-started/overview/first-app?tabs=netcore-cli