我有2个表,父表保存历史表的定义字段。我试图在历史记录表中引用def表的外键,但是当我运行此代码时,被引用的对象始终为null。
我做错了什么?
[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; }
}
答案 0 :(得分:1)
我添加了一个Live Example of this on Gistlyn来显示此工作正常:
您可以将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);
然后使用Load*
APIs to load references,例如:
var row = db.LoadSingleById<PublishHistory>(1);
row.PrintDump();
哪个输出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
}