0

I am using EF Code First in an Asp.net project. Code First created my database just fine but I am having fits getting it to seed my database with data. I am using a custom database initializer like so:

namespace Toolkit.Model
{
    public class EntitiesContextInitializer : DropCreateDatabaseIfModelChanges<ToolkitContext>
    {
        protected override void Seed(ToolkitContext context)
        {
            List<Server> Servers = new List<Server>
            {
                new Server { ServerName = "Server 16" },
                new Server { ServerName = "Server 29" }
            };

            foreach (Server server in Servers)
            {
                context.Servers.Add(server);
            }

            context.SaveChanges();
        }
    }
}

I set the initializer in the the Global.asax.cs and force the database to initialize like so:

using Toolkit.Model;

namespace Toolkit
{
    public class Global : System.Web.HttpApplication
    {

        void Application_Start(object sender, EventArgs e)
        {
            Database.SetInitializer(new EntitiesContextInitializer());
            var context = new ToolkitContext();
            context.Database.Initialize(false);
        }

        ...
}

The problem is that my Initializer never gets called and so my database never gets populated. I have even tried changing the initializer to inherit DropCreateDatabaseAlways instead but still nothing... What am I missing here?

EDIT So after dropping the database my initializer got called fine. However I still wonder why it didn't get called in other instances such as when I added a table (so the model changed) or when I changed the initialzer to be DropCreateDatabaseAlways.

2
  • Is your connection string correct? Are you able to save anything using that context? Dec 21, 2012 at 16:06
  • @IronMan84 Yes I am connecting fine.
    – ferics2
    Dec 21, 2012 at 16:26

4 Answers 4

0

I'm using a similar configuration for a SQL CE database, but instead of DropCreate, I'm using CreateIfNotExists.
The difference between what I have and yours is that you have are creating a context and calling initialize on the database. Based on that, I would suggest that you remove the context setup and the Database.initialize calls that you have in the application_start method.

0

try

Database.SetInitializer<MyContextName>(new EntitiesContextInitializer)

in EF 5.0 the library is declared as

public static void SetInitializer<TContext>(IDatabaseInitializer<TContext> strategy) where TContext : DbContext;
0

You can use Database Migrations with Code-First. Whenever you use Update-database command when your model changes, your Seed method (in Migrations) will be called.

This article explains more:

http://msdn.microsoft.com/en-gb/data/jj591621

You can also force it in Application_Start() your way:

Database.SetInitializer(new EntitiesContextInitializer());
            var context = new ToolkitContext();
            context.Database.Initialize(true);
2
  • I am using database migrations. When I add the POCOs for a new table and do the update the table is created but the seed method is never called on my initializer. The only way I have gotten this to work is to manually drop the existing DB. As for the application start, if have already tried changing initialize to true with no success.
    – ferics2
    Dec 21, 2012 at 18:16
  • It is a actually the Seed method of DbMigrationsConfiguration<ToolkitContext> that is called when you do a migration, not the initializer. Example: odetocode.com/Blogs/scott/archive/2012/08/31/…. Try placing your code there. Dec 21, 2012 at 18:20
0

At the end of your overriden menthod, Seed, in class, EntitiesContextInitializer, place at the end of the method add a line:

base.Seed (context)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.