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...

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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 ?
@DineshN.Samarathunga: I don't know. I'd recommend to ask and describe the problem in a new question.
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())
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.