ormlite-servicestack,C#,ormlite Servicestack" /> ormlite-servicestack,C#,ormlite Servicestack" />

C# ORMLite中的ServiceStack如何对父表进行简单引用?

C# ORMLite中的ServiceStack如何对父表进行简单引用?,c#,ormlite-servicestack,C#,ormlite Servicestack,我有两个表,父表包含历史表的定义字段。我试图在历史记录表中引用def表的外键,但是当我运行此代码时,引用的对象总是空的 我做错了什么 [Alias("DOCMGR_PublishHistories")] public class PublishHistory { [AutoIncrement] [PrimaryKey] public virtual int Id { get; set; } public int DocumentDefinitionId { ge

我有两个表,父表包含历史表的定义字段。我试图在历史记录表中引用def表的外键,但是当我运行此代码时,引用的对象总是空的

我做错了什么

[Alias("DOCMGR_PublishHistories")]
public class PublishHistory
{
    [AutoIncrement]
    [PrimaryKey]
    public virtual int Id { get; set; }

    public int DocumentDefinitionId { get; set; }

    [Reference]
    public DocumentDefinition DocumentDefinition { get; set; }

    [Required]
    public DateTimeOffset RequestedAt { get; set; }

    [StringLength(256)]
    [Required]
    public string RequestedBy { get; set; }

    [Required]
    public DateTimeOffset EffectiveDate { get; set; }
}

[Alias("DOCMGR_DocumentDefinitions")]
public class DocumentDefinition
{
    [AutoIncrement]
    [PrimaryKey]
    public virtual int Id { get; set; }

    [System.ComponentModel.DataAnnotations.StringLength(50)]
    [System.ComponentModel.DataAnnotations.Required]
    public virtual string LegalDocType { get; set; }

    [System.ComponentModel.DataAnnotations.Required]
    [System.ComponentModel.DataAnnotations.StringLength(50)]
    public virtual string LegalDocSubType { get; set; }

    [System.ComponentModel.DataAnnotations.Required]
    [System.ComponentModel.DataAnnotations.StringLength(256)]
    public virtual string DisplayTitle{ get; set; }

    [System.ComponentModel.DataAnnotations.StringLength(50)]
    public virtual string EntityName{ get; set; }

    [System.ComponentModel.DataAnnotations.StringLength(256)]
    public virtual string EntityUrl{ get; set; }

    [System.ComponentModel.DataAnnotations.Required]
    public virtual bool IsActive { get; set; }
}
我添加了一个示例,以显示此功能按预期工作:

您可以将
PublishHistory
表及其
DocumentDefinition
引用保存为:

db.CreateTable<DocumentDefinition>();
db.CreateTable<PublishHistory>();

db.Save(new PublishHistory {
    RequestedBy = "RequestedBy",
    RequestedAt = DateTime.UtcNow,
    EffectiveDate = DateTimeOffset.UtcNow,
    DocumentDefinition = new DocumentDefinition {
        LegalDocType = "LegalDocType",
        LegalDocSubType = "LegalDocSubType",
        DisplayTitle = "DisplayTitle",
        EntityName = "EntityName",
        EntityUrl = "EntityUrl",
        IsActive = true,
    }
}, references: true);
它输出
PublishHistory
表及其填充的
DocumentDefinition
子引用:

{
    Id: 1,
    DocumentDefinitionId: 1,
    DocumentDefinition: 
    {
        Id: 1,
        LegalDocType: LegalDocType,
        LegalDocSubType: LegalDocSubType,
        DisplayTitle: DisplayTitle,
        EntityName: EntityName,
        EntityUrl: EntityUrl,
        IsActive: True
    },
    RequestedAt: 2019-05-07T20:08:29.1437953+00:00,
    RequestedBy: RequestedBy,
    EffectiveDate: 2019-05-07T20:08:29.1437953+00:00
}

在我的例子中,我应该提到我正在返回PublishHistory对象的列表。我每次都需要调用load吗?@PaulDuer
load*
API是加载引用类型的工具,因此当您需要
PublishHistory
类型及其
DocumentDefinition
引用时,您可以在
PublishHistory
上使用OrmLite的其他API,但它们不会加载其引用。
{
    Id: 1,
    DocumentDefinitionId: 1,
    DocumentDefinition: 
    {
        Id: 1,
        LegalDocType: LegalDocType,
        LegalDocSubType: LegalDocSubType,
        DisplayTitle: DisplayTitle,
        EntityName: EntityName,
        EntityUrl: EntityUrl,
        IsActive: True
    },
    RequestedAt: 2019-05-07T20:08:29.1437953+00:00,
    RequestedBy: RequestedBy,
    EffectiveDate: 2019-05-07T20:08:29.1437953+00:00
}