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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:32:13+00:00 2026-06-16T19:32:13+00:00

I have an MVC4 app which I’ve recently upgraded to Entity Framework 5 and

  • 0

I have an MVC4 app which I’ve recently upgraded to Entity Framework 5 and I am trying to move our database over to using migrations from the development style of dropping and creating each run.

Here’s what I’ve done in my app start function.

protected void Application_Start()
{
    Database.SetInitializer(
        new MigrateDatabaseToLatestVersion< MyContext, Configuration >() );
    ...
}

I ran the Enable-Migrations command on my repositories project and I thought that this would create an initial migration file however the only file it created was Configuration

When I delete the database it creates it as expected via code first and seeds the database from the Configuration file. In the configuration file I changed all the Add() functions to AddOrUpdate()

However it runs the seed function in my Configuration file each time the website starts and duplicates all the seed data again and again.

I imagined that it would create an initial migration file as the blog I read suggested that it would and I could put the seed data in there but it didn’t

Can anyone explain how I should be setting up DB in code so that it only seeds once?


LINK: The migrations blog post I followed


While this is quite interesting for using the EF migrate.exe I’ve since switched to using roundhouse for running migrations. I still use EF to scaffold my migrations based on the models but I wrote a little console app to write the migrations out to SQL files. I then use roundhouse to perform the migrations themselves through my rake build scripts. There’s a little more process involved but it’s much more stable than using EF to perform the migrations on the fly when the application starts up.

  • 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-16T19:32:14+00:00Added an answer on June 16, 2026 at 7:32 pm

    This has proved to be a popular post so I have updated it in light of feedback from others. The main thing to know is that the Seed method in the Configuration class is run EVERY time the application starts, which isn’t what the comment in the template method implies. See the answer from someone at Microsoft to this post about why that is – thanks to Jason Learmouth for finding that.

    If you, like me, only want to run the database updates if there are any pending migrations then you need to do a bit more work. You can find that out if there are pending migrations by calling migrator.GetPendingMigrations(), but you have to do that in the ctor as the list of pending migrations is cleared before Seed method is called. The code to implement this, which goes in the Migrations.Configuration class is as follows:

    internal sealed class Configuration : DbMigrationsConfiguration<YourDbContext>
    { 
        private readonly bool _pendingMigrations;
    
        public Configuration()
        {
            // If you want automatic migrations the uncomment the line below.
            //AutomaticMigrationsEnabled = true;
            var migrator = new DbMigrator(this);
            _pendingMigrations = migrator.GetPendingMigrations().Any();
        }
    
        protected override void Seed(MyDbContext context)
        {
            //Microsoft comment says "This method will be called after migrating to the latest version."
            //However my testing shows that it is called every time the software starts
    
            //Exit if there aren't any pending migrations
            if (!_pendingMigrations) return;
    
            //else run your code to seed the database, e.g.
            context.Foos.AddOrUpdate( new Foo { bar = true});
        }
    }
    

    I should point out that some people have suggested putting the seed code in the actual ‘up’ migration code. This works, but means you need to remember to put the seed code in each new migration and its pretty hard remember so I wouldn’t do that. However if your seed changes with each migration then that might be the a good way to go.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making an MVC4 web application using Entity Framework 5 (Database-first with generated
I have a MVC4 C# project using Entity Framework and I want to know
I have an ASP.NET MVC4 app which uses DotNetOpenAuth 4.0.1 to authenticate users with
I am using EF5 codefirst in an ASP.NET MVC4 app. I have products and
I have an MVC4 app, but I'm primarily using it for the WebAPI parts.
I've created a method using the new WebAPI features in MVC4 and have it
I have trying to learn MVC4 Web API. I am running my project from
Using ASP.NET MVC4 I have created a DelegatingHandler in a WebAPI project. I use
I am using mvc4 and have a telerik control grid with editing mode enabled.
I have a ASP.NET MVC4 project with HTML5 semantic markup enabled. When using @Html.EditorFor(m

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.