Skip to content

Instantly share code, notes, and snippets.

@mythz
Created March 30, 2019 00:48
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 mythz/92a3f007e3f5d5b3440408f849b5d0b0 to your computer and use it in GitHub Desktop.
Save mythz/92a3f007e3f5d5b3440408f849b5d0b0 to your computer and use it in GitHub Desktop.
Custom Self Reference Conventions
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
public class CustomerAddress
{
[AutoIncrement]
public int Id { get; set; }
public string Address { get; set; }
public string Country { get; set; }
}
public class Customer
{
[AutoIncrement]
public int Id { get; set; }
public string Name { get; set; }
[References(typeof(CustomerAddress))]
public int Id_PrimaryAddress { get; set; } // with a prefix
[Reference]
public CustomerAddress PrimaryAddress { get; set; }
}
db.CreateTable<Customer>();
db.CreateTable<CustomerAddress>();
var customer = new Customer
{
Name = "The Customer",
PrimaryAddress = new CustomerAddress {
Address = "1 Home Street",
Country = "US"
},
};
db.Save(customer, references:true);
var c = db.LoadSelect<Customer>(x => x.Name == "The Customer");
c.PrintDump();
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="ServiceStack.Text" version="5.2.0" targetFramework="net45" />
<package id="ServiceStack.Interfaces" version="5.2.0" targetFramework="net45" />
<package id="ServiceStack.Common" version="5.2.0" targetFramework="net45" />
<package id="ServiceStack.OrmLite" version="5.2.0" targetFramework="net45" />
<package id="ServiceStack.OrmLite.Sqlite.Windows" version="5.2.0" targetFramework="net45" />
</packages>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment