I am using Umbraco 7.2.1 and wanted to create table on application start. There are two tables project and students, classes are as 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 I am checking if these table exists and if not creating them as below
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
var db = applicationContext.DatabaseContext.Database;
//Check if the DB table does NOT exist
if (!db.TableExist("Projects"))
{
//Create DB table - and set overwrite to false
db.CreateTable<Project>(false);
}
if (!db.TableExist("Students"))
{
//Here is the problem
//If Student table do not contain foreign key, it works fine
db.CreateTable<Student>(false);
}
base.ApplicationStarted(umbracoApplication, applicationContext);
}
Now, the first table is created properly without any error, but when it reaches for second table(Students) there is a null reference error, I know this is due to the foreign key I have used, but do not know how to solve this. Stack trace is as below
[NullReferenceException: Object reference not set to an instance of an 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 to an instance of an 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 to an instance of an 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 to the classes as below and everything went well.