Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8743227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:38:21+00:00 2026-06-13T11:38:21+00:00

I’ve enabled code-first migrations on my entity framework project, and have added several migrations

  • 0

I’ve enabled code-first migrations on my entity framework project, and have added several migrations which do things like rename tables. However, I have now deleted the database and caused entity framework to generate a brand new database based on my latest data model. If I try to run:

PM> Add-Migration TestMigration

… it tells me that I need to apply the existing migrations first. So I run:

PM> Update-Database

… but the trouble is that it’s trying to update a database that doesn’t need updating; it’s already based on the latest data model. So I get an error when it tries to rename a table that now doesn’t exist.

Is there some way I can indicate to data migrations that my database is up-to-date and doesn’t need any migrations running on it? What should I do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:38:22+00:00Added an answer on June 13, 2026 at 11:38 am

    I’ve found a way to indicate that my database is up-to-date, and it’s (unsurprisingly) based on modifying the __MigrationHistory table, which code-first migrations uses to determine which migrations to apply to the DB when you run Update-Database.

    By the way, whilst researching this answer I came across a very nice reference for code-first migrations commands, which can be found at: http://dotnet.dzone.com/articles/ef-migrations-command

    When the database is created from scratch automatically by EF, it will always put a single entry into the __MigrationHistory table, and that entry will have the MigrationId (currentDateTime)_InitialCreate. This represents the initial creation of the database that EF has just carried out. However, your migration history is not going to begin with that MigrationId because you will have started with something else.

    To “fool” code-first migrations into thinking that you’re on the latest migration, you need to delete that (currentDateTime)_InitialCreate entry from the __MigrationHistory table of the newly-created DB, and insert what would have been there if you still had the old DB which had had the migrations applied to it.

    So, first delete everything from the newly-generated DB’s __MigrationHistory table. Then, go into package manager console and run:

    PM> Update-Database -Script
    

    From the resulting SQL script, pull out all the lines beginning with:

    INSERT INTO [__MigrationHistory]...
    

    Then, run those INSERT statements within the context of the newly-created database. Check that each of those rows now exists in your __MigrationHistory table. When you next run:

    PM> Update-Database
    

    … you should get a message saying “No pending code-based migrations.” Congratulations – you’ve fooled code-first migrations into thinking you’re now on the latest migration, and you can continue where you left off adding new migrations from here.

    I do think there should be some automated way of doing this built into EF code-first, though… maybe they should add something like:

    PM> Update-Database -MigrationsTableOnly
    

    … whereby it would clobber the existing entries in the migrations table, and just insert the new entries into migrations history for each migration defined in your project, but not actually try and run the migrations. Ah well.

    UPDATE
    I’ve found a way to automate this nicely, using a custom initializer’s Seed method. Basically the Seed method deletes the existing migration history data when the DB is created, and inserts your migrations history. In my database context constructor, I register the custom initializer like so:

    public class MyDatabaseContext : DbContext {
        public MyDatabaseContext() : base() {
            Database.SetInitializer(new MyDatabaseContextMigrationHistoryInitializer());
        }
    

    The custom initializer itself looks like this:

    /// <summary>
    /// This initializer clears the __MigrationHistory table contents created by EF code-first when it first
    /// generates the database, and inserts all the migration history entries for migrations that have been
    /// created in this project, indicating to EF code-first data migrations that the database is
    /// "up-to-date" and that no migrations need to be run when "Update-Database" is run, because we're
    /// already at the latest schema by virtue of the fact that the database has just been created from
    /// scratch using the latest schema.
    /// 
    /// The Seed method needs to be updated each time a new migration is added with "Add-Migration".  In
    /// the package manager console, run "Update-Database -Script", and in the SQL script which is generated,
    /// find the INSERT statement that inserts the row for that new migration into the __MigrationHistory
    /// table.  Add that INSERT statement as a new "ExecuteSqlCommand" to the end of the Seed method.
    /// </summary>
    public class MyDatabaseContextMigrationHistoryInitializer : CreateDatabaseIfNotExists<MyDatabaseContext> {
        /// <summary>
        /// Sets up this context's migration history with the entries for all migrations that have been created in this project.
        /// </summary>
        /// <param name="context">The context of the database in which the seed code is to be executed.</param>
        protected override void Seed(MyDatabaseContext context) {
            // Delete existing content from migration history table, and insert our project's migrations
            context.Database.ExecuteSqlCommand("DELETE FROM __MigrationHistory");
            context.Database.ExecuteSqlCommand("INSERT INTO __MigrationHistory (MigrationId, Model, ProductVersion) VALUES ('201210091606260_InitialCreate', 0x1F8B0800000000000400ECBD07601C499625262F6DCA7B7F4AF54AD7E074A..., '5.0.0.net40')");
            context.Database.ExecuteSqlCommand("INSERT INTO __MigrationHistory (MigrationId, Model, ProductVersion) VALUES ('201210102218467_MakeConceptUserIdNullable', 0x1F8B0800000000000400ECBD07601C499625262F6DCA7B7F4..., '5.0.0.net40')");
            context.Database.ExecuteSqlCommand("INSERT INTO __MigrationHistory (MigrationId, Model, ProductVersion) VALUES ('201210231418163_ChangeDateTimesToDateTimeOffsets', 0x1F8B0800000000000400ECBD07601C499625262F6D..., '5.0.0.net40')");
            context.Database.ExecuteSqlCommand("INSERT INTO __MigrationHistory (MigrationId, Model, ProductVersion) VALUES ('201210251833252_AddConfigSettings', 0x1F8B0800000000000400ECBD07601C499625262F6DCA7B7F4AF54AD7E..., '5.0.0.net40')");
            context.Database.ExecuteSqlCommand("INSERT INTO __MigrationHistory (MigrationId, Model, ProductVersion) VALUES ('201210260822485_RenamingOfSomeEntities', 0x1F8B0800000000000400ECBD07601C499625262F6DCA7B7F4AF5..., '5.0.0.net40')");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I would like to run a str_replace or preg_replace which looks for certain words
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an autohotkey script which looks up a word in a bilingual dictionary
I have an array which has BIG numbers and small numbers in it. I
I have a text area in my form which accepts all possible characters from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.