Reputation: 1120
I have 2 tables, the parent table holds definition fields for a history table. I am trying to reference the foreign key of the def table in the history table, but when I run this code, the referenced object is always null.
What did I do wrong?
[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; }
}
Upvotes: 2
Views: 139
Reputation: 143284
I've added a Live Example of this on Gistlyn showing this working as expected:
You can save the PublishHistory
table with its DocumentDefinition
reference with:
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);
Then use the Load*
APIs to load references, e.g:
var row = db.LoadSingleById<PublishHistory>(1);
row.PrintDump();
Which outputs the PublishHistory
table and its populated DocumentDefinition
child reference:
{
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
}
Upvotes: 1