3

For some reason my DB is not being created and i'm not getting any errors. I'm using "SQL Server"

Tring to initialize the DB in the global.asax.ca file:

protected void Application_Start(object sender, EventArgs e)
{
     //BREAKPOINT HITS. GOOD
     Database.SetInitializer<MenuManagerContext>(new MenuManagerServiceInitializer()); 
}

...

public class MenuManagerServiceInitializer : CreateDatabaseIfNotExists<MenuManagerContext>
{
    //BREAKPOINT NEVER HITS. BAD
    protected override void Seed(MenuManagerContext context)
    {
        context.Chains.Add(...);

        context.SaveChanges();

        base.Seed(context);
    }
}

Any ideas why the database is not being created? I'm not even getting errors so it's very hard to tell what is wrong...

vidalsasoon
  • 4,365
  • 1
  • 32
  • 40

2 Answers2

8

Database.SetInitializer doesn't cause the database to be created. It's only setting the initialization strategy.

The DB is created if you are using a context for the very first time, for instance by issuing any query, or by attaching or adding an object to the context, and so on.

Slauma
  • 175,098
  • 59
  • 401
  • 420
  • I have similer case as above. Before it works fine. lat time I added about 15 entities. After that db creation stops. No errors pops up. no db created. But ONModelCreating is get called in context. How do I know the reason to happen this ? – DineshNS Apr 22 '12 at 07:26
  • 1
    @DineshN.Samarathunga: I don't know. I'd recommend to ask and describe the problem in a new question. – Slauma Apr 22 '12 at 11:06
1

Make a call to an Action that returns data through your context

public ActionResult Index()
{
    //should trigger call to MenuManagerServiceInitializer.Seed()
    return View(new MenuManagerContext().Chains.ToList())
}
lcranf
  • 171
  • 1
  • 1
  • 6