Skip to main content
 首页 » 编程设计

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

2024年06月03日15youxin

我正在使用 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; } 
}