应用程序启动时在 Umbraco 中使用外键创建 table 的空引用

Null reference on creating table with foreign key in Umbraco on application start

我正在使用 Umbraco 7.2.1 并希望在应用程序启动时创建 table。有两个tables项目和学生,类如下

[TableName("Projects")]
public class Project
{
    [PrimaryKeyColumn(AutoIncrement=true)]
    public int Id { get; set; }

    [Required]
    public string Name { get; set; }
}

[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; }
}

在应用程序启动时,我正在检查这些 table 是否存在,如果不存在则如下创建它们

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

现在,第一个table创建正确没有任何错误,但是当它到达第二个table(学生)时出现空引用错误,我知道这是由于外国key我用过,不知道怎么解决。堆栈跟踪如下

[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

我向 类 添加了属性主键,如下所示,一切顺利。

[TableName("Projects")]
[PrimaryKey("Id", autoIncrement = true)] // This was missing
public class Project
{
    [PrimaryKeyColumn(AutoIncrement=true)]
    public int Id { get; set; }

    [Required]
    public string Name { get; set; }
}


[TableName("Students")]
[PrimaryKey("Id", autoIncrement = true)] // this was missing
//, but only affected class with 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; }
}