Skip to content

Instantly share code, notes, and snippets.

@mythz mythz/main.cs
Created Mar 30, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.