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 8793639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:08:53+00:00 2026-06-13T23:08:53+00:00

We am using code first EF 4.3.1 with database migrations. Sometimes we drop the

  • 0

We am using code first EF 4.3.1 with database migrations. Sometimes we drop the database and let EF recreate it, mainly for local development purposes.

There is a procedure which applies pending migrations and when the database is recreated the pending migrations aren’t necessary and error is thrown when EF tries to apply them.

I check the migration date from the string and if this is before the database creation date then I manually add it to the __migrationHistory table and don’t apply the migration.

This covers most of the scenarios except when someone recreates the database, updates from source control and retrieves oustanding migrations which are before the database creation date.

I have also tried using a try catch around the dbMigrator.Update(migration) method call but if one migrations fails, further ones won’t be applied as it remembers the exceptions.

Has anyone found a way to get around this issue?

This is the method I have written:

private static void ApplyMigration<T, TU>()
where T : DbContext, new()
where TU : DbMigrationsConfiguration, new()
{
    var migrationsAlreadyApplied = new List<string>();
    var dbMigrator = new DbMigrator(new TU());

    var creationMigrationId = dbMigrator.GetDatabaseMigrations().Single(m => m.Contains("InitialCreate"));
    var dbDateCreated = DateTime.ParseExact(creationMigrationId.Substring(0, 12), Constants.MigrationDateFormat, CultureInfo.InvariantCulture);

    dbMigrator.GetPendingMigrations().ToList()
                                    .ForEach(migration =>
                                    {
                                        var migrationDate = DateTime.ParseExact(migration.Substring(0, 12), Constants.MigrationDateFormat, CultureInfo.InvariantCulture);

                                        if (migrationDate > dbDateCreated)
                                            dbMigrator.Update(migration);
                                        else
                                            migrationsAlreadyApplied.Add(migration);
                                    });

    using (var dbContext = new T())
    {
        foreach(var migration in migrationsAlreadyApplied)
            dbContext.Database.ExecuteSqlCommand("insert into __MigrationHistory "
                      + "select '" + migration + "', Model, ProductVersion "
                      + "from __MigrationHistory "
                      + "where MigrationId = '" + migration + "'");
    }
}
  • 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-13T23:08:54+00:00Added an answer on June 13, 2026 at 11:08 pm

    I have now resolved this.

    In the seed method of my dbContext initialiser, I call a method which will populate the migration history manually. You can then use the ApplyMigrations method to call the migrations normally.

    public class UserEntitiesContextInitializer : CreateDatabaseIfNotExists<UserEntitiesContext>
    {
        protected override void Seed(UserEntitiesContext context)
        {
            // Update migration history with existing migrations to prevent EF recognising them as pending migrations
            DatabaseAdministration.UpdateMigrationHistory<UserEntitiesContext, UserEntitiesContextConfiguration>();
        }
    }
    
    public static void UpdateMigrationHistory<T, TU>()
            where T : DbContext, new()
            where TU : DbMigrationsConfiguration, new()
        {
            using (var dbContext = new T())
            {
                var dbMigrator = new DbMigrator(new TU());
    
                var creationMigrationId = dbMigrator.GetDatabaseMigrations().Single(m => m.Contains("InitialCreate"));
    
                foreach (var migration in dbMigrator.GetPendingMigrations())
                    dbContext.Database.ExecuteSqlCommand("insert into __MigrationHistory "
                                                            + "select '" + migration + "', Model, ProductVersion "
                                                            + "from __MigrationHistory "
                                                            + "where MigrationId = '" + creationMigrationId + "'");
            }
        }
    
    private static void ApplyMigration<T, TU>()
            where T : DbContext, new()
            where TU : DbMigrationsConfiguration, new()
        {
            var dbMigrator = new DbMigrator(new TU());
    
            if (dbMigrator.GetPendingMigrations().Any())
                dbMigrator.Update(dbMigrator.GetPendingMigrations().Last());
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I successfully created database on my local server using Code First, migrated it to
I am using EF Code First to create a database on local .\SQLEXPRESS. Among
I've been using Code-First Migrations Beta up until now and when migrating my database
I'm using EF Code First 4.3 Migrations to update my database scheme. Now I
Easier to show by example -- I'm using code-first to construct a database. I
I'm using the Code First approach to build the database in this problem. I
I'm using Entity Framework 4.3 Code First with a custom database initializer like this:
I am using mvc3 to develop an database application and followed the Code-First Tutorial
I am creating a new database model using Entity Framework 4.3 Code-FIrst; using Fluent
I am using Code-First migrations. Now I want my application when published to a

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.