EF6 OnModelCreating() event doesnt

asp.net-mvc entity-framework entity-framework-6

Question

I am following along to this tutorial on EF6 and Codefirst. http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application

I started a solution and just added the Models, the contexts and the initializer

namespace TestContoso.DAL
{
    public class SchoolContext : DbContext
    {
        public SchoolContext()
            : base("Name=SchoolContextDB")
        {

        }

        public DbSet<Student> Students { get; set; }
        public DbSet<Enrollment> Enrollments { get; set; }
        public DbSet<Course> Courses { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
    }
}

and created a dummy Home Controller and Index view.

namespace TestContoso.Controllers
    {
        public class HomeController : Controller
        {
            private SchoolContext db = new SchoolContext();
            //
            // GET: /Home/
            public ActionResult Index()
            {
                return View();
            }
        }
    }

I have also set the Connection String and set the initializer in web.config

  <entityFramework>
    <contexts>
      <context type="TestContoso.DAL.SchoolContext, TestContoso">
        <databaseInitializer type="TestContoso.DAL.SchoolInit, TestContoso"/>
      </context>
    </contexts>

When I run the solution, my expectation is that the database would get created, however I notice that the OnModelCreating event never fires so the db is never created. Why isn't the db getting created? How would I cause the Code First migration to fire?

1
0
3/21/2015 1:06:40 AM

Accepted Answer

The OnModelCreating event only seemed to get fired when there was some query querying that model, to test that I just added a dummy query in the Index controller and it worked.

    public class HomeController : Controller
    {
        private SchoolContext db = new SchoolContext();
        //
        // GET: /Home/
        public ActionResult Index()
        {
            var students = from s in db.Students
                               select s;

            return View();
        }
    }
}
0
3/21/2015 3:58:32 PM

Popular Answer

The article gives you an alternative. Try the alternative method of adding the initialization strategy via code to see if that works.

As an alternative to setting the initializer in the Web.config file is to do it in code by adding a Database.SetInitializer statement to the Application_Start method in in the Global.asax.cs file.

Database.SetInitializer(new CreateDatabaseIfNotExists<SchoolContext>());

And as specified in the comments. Run a query against the data or follow the section on 'Set up EF to initialize the database with test data' to automatically seed the database.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow