EF6 OnModelCreating() event doesnt

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

Accepted Answer

I just added a test query to the Index controller to see if the OnModelCreating event was only emitted when a query was being run against that model, and it was.

    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

You have a choice, according to the article. To see if it works, try the alternate approach of adding the initialization strategy via code.

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>());

Additionally, as noted in the comments. To automatically seed the database, run a query against the data or follow the instructions in the section titled "Set up EF to establish the database with test data."



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