c# - 应用程序启动时在 Umbraco 中使用外键创建表的空引用

标签 c# umbraco umbraco7 petapoco

我正在使用 Umbraco 7.2.1 并希望在应用程序启动时创建表。有project和students两张表,classes如下。

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

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

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

现在,第一个表正确创建,没有任何错误,但是当它到达第二个表(Students)时出现空引用错误,我知道这是由于我使用的外键,但不知道如何解决这个问题。堆栈跟踪如下

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

关于c# - 应用程序启动时在 Umbraco 中使用外键创建表的空引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30845461/

相关文章:

c# - 编写正则表达式以搜索子字符串 C#

c# - selenium webdriver C# 多线程

c# - ToolStrip.Background 仅更改按钮的背景 (C#/.NET/WinForms)

c# - 使 Visual Studio 2012 在一个解决方案中查找并运行 x64 和 x86 单元测试

asp.net - 是否可以在 ASP.NET Core API 中运行 Umbraco?

c# - 如何使用 Umbraco 7 在事务中执行内容更新?

c# - 如何将razor生成的html变成字符串?

javascript - 在 umbraco 7.2.5 中实现标签云和唯一名称验证

javascript - Umbraco 7 - 语法帮助 - JS 还是 Angular? - 循环浏览 Umbraco 属性编辑器

tinymce - 富文本编辑器 (tinymce) 图像 - 如何禁用自动高度和宽度?