asp.net mvc - Database Not Being Created In Code First Approach -
i have seen few questions this, none seem answer question - common response seems 'lazy loading - database/tables created when try access them' in case am:
dim students list(of student) = db.students.tolist the above throwing error, because students table empty/null.
here (very simple) schoolcontext:
imports system.data.entity public class schoolcontext inherits dbcontext public students dbset(of student) end class here schoolinitialiser class:
imports system.data.entity public class schoolinitializer inherits dropcreatedatabasealways(of schoolcontext) protected overrides sub seed(context schoolcontext) dim students list(of student) = new list(of student) { new student("jessica", "jones"), new student("chuck", "norris"), new student("rambo", "john") } each student in students context.students.add(student) next context.savechanges() end sub end class and connection string in web.config:
<add name="schoolcontext" connectionstring="data source= (localdb)\mssqllocaldb;attachdbfilename=|datadirectory|\school.mdf;initial catalog=aspnet-webapplication1-20160205092922;integrated security=true" providername="system.data.sqlclient" /> and global.asax calling setinitialize function
imports system.data.entity imports system.web.optimization public class mvcapplication inherits system.web.httpapplication protected sub application_start() database.setinitializer(new schoolinitializer) arearegistration.registerallareas() filterconfig.registerglobalfilters(globalfilters.filters) routeconfig.registerroutes(routetable.routes) bundleconfig.registerbundles(bundletable.bundles) end sub end class no database seems created within appdata folder , far can tell, set okay? suggestions?
i following following tutorial: https://www.youtube.com/watch?v=vatvv1q7ufm
thanks!
so figured 1 out, in case comes across similar problem. using vb, imagine things got lost in translation c#.
there few issues this. first 1 studentcontext class needed properties tables, not global variables:
'not public students dbset(of student) public property students() dbset(of student) after this, errors easier figure out. second error due not having key on student model, added this:
<key()> public property _studentid integer to both property , global variable. next, not connecting database reason, had download , install localdb.
finally, complaining didn't have parameterless constructor. had add in empty constructor in student model.
finally - working (yay)
Comments
Post a Comment