c# - Null reference on creating table with foreign key in Umbraco on application start -
i using umbraco 7.2.1 , wanted create table on application start. there 2 tables project , students, classes follows.
[tablename("projects")] public class project { [primarykeycolumn(autoincrement=true)] public int id { get; set; } [required] public string name { get; set; } } and
[tablename("students")] public class student { [primarykeycolumn(autoincrement=true)] public int id { get; set; } [required] public string name { get; set; } [foreignkey(typeof(project))] public int projectid { get; set; } } on application start checking if these table exists , if not creating them below
protected override void applicationstarted(umbracoapplicationbase umbracoapplication, applicationcontext applicationcontext) { var db = applicationcontext.databasecontext.database; //check if db table not exist if (!db.tableexist("projects")) { //create db table - , set overwrite false db.createtable<project>(false); } if (!db.tableexist("students")) { //here problem //if student table not contain foreign key, works fine db.createtable<student>(false); } base.applicationstarted(umbracoapplication, applicationcontext); } now, first table created without error, when reaches second table(students) there null reference error, know due foreign key have used, not know how solve this. stack trace below
[nullreferenceexception: object reference not set instance of object.] umbraco.core.persistence.databasemodeldefinitions.definitionfactory.getforeignkeydefinition(type modeltype, propertyinfo propertyinfo, foreignkeyattribute attribute, string columnname, string tablename) +203 umbraco.core.persistence.databasemodeldefinitions.definitionfactory.gettabledefinition(type modeltype) +754 umbraco.core.persistence.petapocoextensions.createtable(database db, boolean overwrite, type modeltype) +100 umbraco.core.persistence.petapocoextensions.createtable(database db, boolean overwrite) +121 chatumbraco.app_code.umbracostartup.applicationstarted(umbracoapplicationbase umbracoapplication, applicationcontext applicationcontext) in d:\ecm\projects\umbraco\chatumbraco\chatumbraco\app_code\umbracostartup.cs:44 umbraco.core.applicationeventhandler.onapplicationstarted(umbracoapplicationbase umbracoapplication, applicationcontext applicationcontext) +62 umbraco.core.corebootmanager.<complete>b__5(iapplicationeventhandler x) +79 umbraco.core.enumerableextensions.foreach(ienumerable`1 items, action`1 action) +204 umbraco.core.corebootmanager.complete(action`1 aftercomplete) +185 umbraco.web.webbootmanager.complete(action`1 aftercomplete) +74 umbraco.core.umbracoapplicationbase.startapplication(object sender, eventargs e) +241 umbraco.core.umbracoapplicationbase.application_start(object sender, eventargs e) +40 [httpexception (0x80004005): object reference not set instance of object.] system.web.httpapplicationfactory.ensureappstartcalledforintegratedmode(httpcontext context, httpapplication app) +9905705 system.web.httpapplication.registereventsubscriptionswithiis(intptr appcontext, httpcontext context, methodinfo[] handlers) +118 system.web.httpapplication.initspecial(httpapplicationstate state, methodinfo[] handlers, intptr appcontext, httpcontext context) +172 system.web.httpapplicationfactory.getspecialapplicationinstance(intptr appcontext, httpcontext context) +336 system.web.hosting.pipelineruntime.initializeapplication(intptr appcontext) +296 [httpexception (0x80004005): object reference not set instance of object.] system.web.httpruntime.firstrequestinit(httpcontext context) +9885060 system.web.httpruntime.ensurefirstrequestinit(httpcontext context) +101 system.web.httpruntime.processrequestnotificationprivate(iis7workerrequest wr, httpcontext context) +254
i added attribute primary key classes below , went well.
[tablename("projects")] [primarykey("id", autoincrement = true)] // missing public class project { [primarykeycolumn(autoincrement=true)] public int id { get; set; } [required] public string name { get; set; } } [tablename("students")] [primarykey("id", autoincrement = true)] // missing //, affected class foreign key public class student { [primarykeycolumn(autoincrement=true)] public int id { get; set; } [required] public string name { get; set; } [foreignkey(typeof(project))] public int projectid { get; set; } }
Comments
Post a Comment