Created
May 7, 2019 20:05
-
-
Save mythz/58b8111c74c300f66281b0dc0a5c90ee to your computer and use it in GitHub Desktop.
OrmLite Self Reference Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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