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

  • Home
  • SEARCH
  • 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 8026319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:27:09+00:00 2026-06-04T23:27:09+00:00

I am using entity framework code first and also I have data seeding code

  • 0

I am using entity framework code first
and also I have data seeding code

Now when I run my application my database gets generated but is not seeded with my dummy data.
I have to run entity framework once more to get all data populated.

Any idea why and how to fix that so i do not have to run my app 2x to get database and data?

thnx

my context definition file is:

 public class Context : DbContext
    {
        public DbSet<Task> Tasks { get; set; }
        public DbSet<Agency> Agency { get; set; }

      protected override void OnModelCreating(DbModelBuilder modelBuilder)
     {
        base.OnModelCreating(modelBuilder);
     }
    }

And here is my seeding file

public class Configuration : DbMigrationsConfiguration<Context>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
        AutomaticMigrationDataLossAllowed = true;
    }

    protected override void Seed(Context context)
    {
        GenerateTasks(context);
        context.SaveChanges();
    }

    private static void GenerateTasks(Context context)
    {
        if (context.Task.Any()) return;
        context.Task.Add(new Task() { Name = "Received" });            
    }
}

And hook to create database is:

  Database.SetInitializer(new MigrateDatabaseToLatestVersion<Context, Configuration>());
  var context = new Context();
  context.Database.Initialize(true);
  • 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-04T23:27:11+00:00Added an answer on June 4, 2026 at 11:27 pm

    If your app is an ASP.NET one and is a seperate assembly from the data layer, then you can, instead of configuring the initialization like you did, configure it directly in the web.config. Maybe this is the reason for your problem.

    So if it’s an ASP.NET app, you can try the following:

    (1)

    Comment this out:

    Database.SetInitializer(new MigrateDatabaseToLatestVersion<Context, Configuration>());
    var context = new Context();
    context.Database.Initialize(true);
    

    (2)

    In web.config, insert this right at the end before the closing /configuration tag:

    <entityFramework>
    <contexts>
      <context type="**fully-qualified name of your context class**,
                     **assembly name of your context**">
        <databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[**fully-qualified name of your context class**, **assembly name of your context**],
                                   [**fully-qualified configuration type**, **assembly name of your context**, Version=1.0.0.0, Culture=neutral]], EntityFramework"/>
      </context>
    </contexts>
    

    where fully-qualified configuration type is the class with your migrations-configuration (something like […]Context.Migrations.Configuration)

    I use this configuration-approach for myself in my projects and it works well!

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

Sidebar

Related Questions

We have a project using Entity Framework 4.1 Code First for data storage. It
I'm creating an application where using the Entity framework using code-first. I have created
I'm using Entity Framework Code First v 4.3.0 I have a two entities with
I am creating a new database model using Entity Framework 4.3 Code-FIrst; using Fluent
I am using Entity Framework Code First. I want to query entites from database
I'm creating a application using entity framework code-first and i'm facing some problems with
I have been using Entity Framework CTP with Code-First as in this tutorial by
I'm creating a database using entity framework code first and I'm having some issues
We are using Entity Framework 4.3 Code First. We have three databases. For each
I'm building my application with Entity Framework (model first principle). I'm also using MS

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.