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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:53:51+00:00 2026-06-03T10:53:51+00:00

How can I create (seed) a lot of static data in Entity Framework? By

  • 0

How can I create (seed) a lot of static data in Entity Framework? By static data I mean data that is located in the database, but only exists once.

For instance in my database, I have a CountrySet, containing all the countries on the globe. I would like these countries to always be there, but the fact is that every time I wipe out my database and recreate it for new deployment, all that data is lost.

Creating it all when my application starts (if it’s not already created) appears to be quite time sensitive.

What can I do here?

  • 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-03T10:53:54+00:00Added an answer on June 3, 2026 at 10:53 am

    First of all, let me ask this:

    Are you familiar with the Seed function which you gain access to when you set your own Initializer?

    Example:

    In your main function write something like this:

    System.Data.Entity.Database.SetInitializer(new SystemContext.SystemInitializer());
    

    Where SystemContext is your DbContext Class.

    And in that class you should have something like:

    public class SystemContext : DbContext
        {
            public DbSet<Stock> Stocks { get; set; }
            public DbSet<RawDayValues> StockRawValues { get; set; }
    
            public SystemContext()
                : base("Server=localhost;Database=test;Trusted_Connection=True;") 
            { }
    
            public class SystemInitializer : DropCreateDatabaseIfModelChanges<SystemContext>
            {
                #region SEED
                // This is what you're looking for!
                protected override void Seed(SystemContext context)
                {
                    try
                    {
                        var stock = new Stock() { Symbol = "TEST" };
                        context.Stocks.Add(stock);
                        context.SaveChanges();
                    }
                    catch (Exception ex) { Console.Error.WriteLine(ex.Message); }
                    base.Seed(context);
                }
    
                #endregion
            }
        }
    

    That’s just some template code from a project I had lying around.

    If you’re indeed re-creating your database every time then I don’t know of any other way to “seed” a database without “manually” adding in all the data.

    On the other hand, if you plan on doing something other than dropping the database every time you want to change the details in it, you might have to manually write functions that clear out all the new data according to some logic.

    I hope this answers your question 🙂

    ALSO: You didn’t specify neither in the question nor using tags which technology you’re talking about. I assumed it to be c#, .net and entity-framework-4.3.1. If that is not the case, please re-tag / specify.

    Have a nice day 🙂

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

Sidebar

Related Questions

I can create a contact that is not mail enabled, but how do I
One can create an anonymous object that is initialized through constructor parameters, such as
I can create an array of buttons in Windows Form but how can i
I can create new Silverlight App with .NET RIA Services enabled. But when I
I have an XML file containing seed data that I'm trying to load into
I would like to implement a custom database initialization strategy so that I can
I've followed Adam's answer here and the Entity Framework now works and the Seed()
Possible Duplicate: How to create my own JavaScript Random Number generator that I can
How can create a view with mutiple images, something similiar to the photo app
How can create a New user in ORACLE with full access (alter, delete, select,

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.