Created
March 30, 2019 00:48
-
-
Save mythz/92a3f007e3f5d5b3440408f849b5d0b0 to your computer and use it in GitHub Desktop.
Custom Self Reference Conventions
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 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(); |
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.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