我在我的应用程序中有一个类,它从一个基类继承属性,该基类也首先使用代码映射到一个表。
当我查看数据库时,一切都按预期工作,所有值都填充在我期望的位置,但我的应用程序正在崩溃。
当检查作为模型检索并传递给视图的记录时,我可以看到所有者和创建者字段是空白的,即使在数据库中的继承表上我可以看到 ID 填充了有效的用户 ID...为什么会这些没有被映射?
基类
public class SiteModel
{
public SiteModel() { }
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public string EntityType { get; set; }
public DateTime Created { get; set; }
[ForeignKey("Creator")]
public int CreatorID;
public SiteAccount Creator { get; set; }
[ForeignKey("Owner")]
public int OwnerID;
public SiteAccount Owner { get; set; }
[ForeignKey("Parent")]
public int? ParentID;
public SiteModel Parent { get; set; }
public List<SiteModel> Children { get; set; }
public SiteModel(SiteAccount creator)
{
Creator = creator;
Owner = creator;
Created = DateTime.Now;
EntityType = this.GetType().Name;
}
}
子类
[Table("BlogPost")]
public class BlogPost : SiteModel
{
public BlogPost() { }
public BlogPost(string title, string content, string description, string tags, BlogCategory category, BlogStatus status, SiteAccount creator) : base(creator)
{
Title = title;
Content = content;
Description = description;
Tags = tags;
Parent = category;
Status = status;
}
[Required]
public string Title { get; set; }
[Required]
public string Content { get; set; }
public string Description { get; set; }
[Required]
public string Tags { get; set; }
public BlogStatus Status { get; set; }
}