Skip to content

Instantly share code, notes, and snippets.

@mythz mythz/main.cs
Created May 7, 2019

Embed
What would you like to do?
OrmLite Self Reference Example
using System;
using ServiceStack;
using ServiceStack.Text;
using ServiceStack.OrmLite;
using ServiceStack.OrmLite.Sqlite;
using ServiceStack.DataAnnotations;
var dbFactory = new OrmLiteConnectionFactory(":memory:", SqliteDialect.Provider);
var db = dbFactory.Open(); // Open ADO.NET DB Connection
[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; }
}
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);
var row = db.LoadSingleById<PublishHistory>(1);
row.PrintDump();
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="ServiceStack.Text" version="5.5.0" targetFramework="net45" />
<package id="ServiceStack.Interfaces" version="5.5.0" targetFramework="net45" />
<package id="ServiceStack.Common" version="5.5.0" targetFramework="net45" />
<package id="ServiceStack.OrmLite" version="5.5.0" targetFramework="net45" />
<package id="ServiceStack.OrmLite.Sqlite.Windows" version="5.5.0" targetFramework="net45" />
</packages>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.