0

I have a strange problem this morning.

I followed this turotial : http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application ...

... on how to implement code first in your projects. I did the same tutorial 2 weeks ago and everything worked great. Since then, I made some changes and upgraded to MVC 4, and now Code First has stop working.

I tried the same tutorials, with exact same step I did before in a MVC3 web app, Drop the database, re-re-recreated it, and the "Seed" method Or the "OnModelCreating" in the DAL don't seems to be called any more.

I have my Database.SetInitializer(new AppInitializer()); In my Application_Start(), which is called, but after that nothing happens.

Somebody knows why this could have stop working suddenly? MVC4?

Thank you!

Robert Levy
  • 28,747
  • 6
  • 62
  • 94
iPeo
  • 399
  • 2
  • 7
  • 16

4 Answers4

2

When the context changes you could either drop the database and create it manually which I don't believe you want to do that if you're using EF Code First approach, or you could use what you said calling Database.SetInitializer() method on your application start.

protected void Application_Start() {
    DbDatabase.SetInitializer(new 
        DropCreateDatabaseIfModelChanges<MyApp.Models.MyContext>());
    // ...
}

As for the Seed() method here is how I would do it

public class MyContextInitializer :
    DropCreateDatabaseIfModelChanges <MyApp.Models.MyContext>
{
    protected override void Seed(MyApp.Models.MyContext context)
        {
            base.Seed(context);

            context.Tags.Add(new Tag
            {
                    Name = ".NET",
                    Description = "Microsoft's .NET language related"
            });

            context.SaveChanges();
        }
}

Once you did that you could change your DbDatabase.SetInitializer() method call like this:

protected void Application_Start() {
    DbDatabase.SetInitializer(new MyContextInitializer());
    // ...
}

Note that you can use your class as long as it implements an IDatabaseInitializer.

One last thing about SQL Server is to be sure that you don't have any opened connection to the provider because it won't be able to drop the database.

Esteban
  • 3,108
  • 3
  • 32
  • 51
2

What could happened is that you have run the application for the first time without calling Database.SetInitializer(...). At this point database was created. But since you haven't done any changes to your model database doesn't need to be created or initialized - override void Seed(...) method on the INITIALIZER is not called.

Try to make some dummy change in your model and check whether it works.

Notice that override void OnModelCreating(...) is called when your data context is accessed for the first time. So when your home page doesn't access database you should manually navigate to same page that does.

vajanko
  • 329
  • 1
  • 2
  • 9
1

I had the same problem when I created a new MVC4 project. It turned out to be because the InitializeSimpleMembershipAttribute calls Database.SetInitializer<UsersContext>(null);. Have a look for other calls to InitializeSimpleMembershipAttribute in your project that aren't your own...

Rob Fonseca-Ensor
  • 15,510
  • 44
  • 57
0

Did you try to convert your working MVC 3 project to MVC 4/Beta or did you create a new MVC 4/Beta project and follow the tutorial? For MVC 4 Beta Intro to ASP.NET MVC and Intro to ASP.NET MVC 4 Beta with Visual Studio 11 Beta I started with scratch. You'll have to do some debugging to see what you did wrong, MVC 4/Beta does not break EF. I hope to update Tom's excellent EF/MVC tutorial to MVC 4 about 4 July 2012

RickAndMSFT
  • 20,912
  • 8
  • 60
  • 78