开发者

Entity Framework "Code First" not seeding or creating databases - no errors

Fo开发者_运维百科r 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...


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.


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())
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜