asp.net mvc - EF6 OnModelCreating() event doesnt -
i following along tutorial on ef6 , 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 solution , added models, contexts , 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 dummy home controller , index view.
namespace testcontoso.controllers { public class homecontroller : controller { private schoolcontext db = new schoolcontext(); // // get: /home/ public actionresult index() { return view(); } } }
i have set connection string , set initializer in web.config
<entityframework> <contexts> <context type="testcontoso.dal.schoolcontext, testcontoso"> <databaseinitializer type="testcontoso.dal.schoolinit, testcontoso"/> </context> </contexts>
when run solution, expectation database created, notice onmodelcreating event never fires db never created. why isn't db getting created? how cause code first migration fire?
the article gives alternative. try alternative method of adding initialization strategy via code see if works.
as alternative setting initializer in web.config file in code adding database.setinitializer statement application_start method in in global.asax.cs file.
database.setinitializer(new createdatabaseifnotexists<schoolcontext>());
and specified in comments. run query against data or follow section on 'set ef initialize database test data' automatically seed database.
Comments
Post a Comment