Skip to content

Instantly share code, notes, and snippets.

@gistlyn
Last active November 10, 2020 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gistlyn/44638e3bd0f5b6057b6f1109a33439ef to your computer and use it in GitHub Desktop.
Save gistlyn/44638e3bd0f5b6057b6f1109a33439ef to your computer and use it in GitHub Desktop.
Customize Tables using Attributes
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();
[Schema("TheSchema")]
[Alias("TableAlias")]
public class CustomTable
{
[PrimaryKey]
[AutoIncrement]
public int CustomKey { get; set; }
[Alias("RDBMS_NAME")]
public string CSharpName { get; set; }
[Index(Unique = true)]
public string IndexColumn { get; set; }
[Default(100)]
public int? DefaultValue { get; set; }
[Default(OrmLiteVariables.SystemUtc)]
public DateTime CurrentDate { get; set; }
[Required]
[StringLength(3)]
public string RequiredCustomLength { get; set; } //= NOT NULL
[DecimalLength(18,4)]
public decimal? CustomDecimalPrecision { get; set; }
[CustomField("DECIMAL(18,4)")]
public decimal? CustomProperty { get; set; }
[Ignore]
public int IgnoredProperty { get; set; } // Completely ignored in OrmLite (used in Serialization only)
[CustomSelect("CustomKey + DefaultValue")]
public int SelectOnlyProperty { get; set; } // Doesn't exist on Table, only used in SELECT Statements
}
db.CreateTable<CustomTable>();
var id = db.Insert(new CustomTable { CSharpName = "Name", IndexColumn = "bar", RequiredCustomLength = "foo",
CustomDecimalPrecision = 1.111m, CustomProperty = 2.222m }, selectIdentity:true);
var customTableRow = db.SingleById<CustomTable>(id);
$"Custom Table Row: {customTableRow.Dump()}".Print();
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="System.Memory" version="4.5.4" targetFramework="net45" />
<package id="ServiceStack.Text" version="5.10.0" targetFramework="net45" />
<package id="ServiceStack.Client" version="5.10.0" targetFramework="net45" />
<package id="ServiceStack.Common" version="5.10.0" targetFramework="net45" />
<package id="ServiceStack.Interfaces" version="5.10.0" targetFramework="net45" />
<package id="ServiceStack.OrmLite" version="5.10.0" targetFramework="net45" />
<package id="ServiceStack.OrmLite.Sqlite.Windows" version="5.10.0" targetFramework="net45" />
</packages>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment