Skip to content

Instantly share code, notes, and snippets.

@gistlyn
Last active Nov 10, 2020
Embed
What would you like to do?
OrmLite Reference Test Data
using System.Collections.Generic;
using System.Data;
using ServiceStack;
using ServiceStack.OrmLite;
using ServiceStack.DataAnnotations;
public class Artist
{
public int Id { get; set; }
public string Name { get; set; }
[Reference]
public List<Track> Tracks { get; set; }
public override string ToString() => Name;
}
public class Track
{
[AutoIncrement]
public int Id { get; set; }
public string Name { get; set; }
public int ArtistId { get; set; }
public string Album { get; set; }
public int Year { get; set; }
public override string ToString() => Name;
}
var Artists = new [] {
new Artist {
Id = 1, Name = "Faith No More",
Tracks = new List<Track> {
new Track { Name = "Everythings Ruined", Album = "Angel Dust", Year = 1992 },
new Track { Name = "Ashes to Ashes", Album = "Album of the Year", Year = 1997 },
}
},
new Artist {
Id = 2, Name = "Live",
Tracks = new List<Track> {
new Track { Name = "Lightning Crashes", Album = "Throwing Copper", Year = 1994 },
new Track { Name = "Lakini's Juice", Album = "Secret Samadhi", Year = 1997 },
}
},
new Artist {
Id = 3, Name = "Nirvana",
Tracks = new List<Track> {
new Track { Name = "Smells Like Teen Spirit", Album = "Nevermind", Year = 1991 },
new Track { Name = "Heart-Shaped Box", Album = "In Utero", Year = 1993 },
}
},
new Artist {
Id = 4, Name = "Pearl Jam",
Tracks = new List<Track> {
new Track { Name = "Alive", Album = "Ten", Year = 1991 },
new Track { Name = "Daughter", Album = "Vs", Year = 1993 },
}
},
};
IDbConnection CreateArtistAndTrackTablesWithData(IDbConnection db)
{
db.DropAndCreateTable<Artist>();
db.DropAndCreateTable<Track>();
Artists.Each(x => db.Save(x, references:true));
return db;
}
using System;
using System.Linq;
using System.Collections.Generic;
using ServiceStack;
using ServiceStack.Text;
using ServiceStack.OrmLite;
using ServiceStack.OrmLite.Sqlite;
var dbFactory = new OrmLiteConnectionFactory(":memory:", SqliteDialect.Provider);
var db = dbFactory.OpenDbConnection();
//DROP and CREATE Artist and Track Tables from their POCO definition
db.DropAndCreateTable<Artist>();
db.DropAndCreateTable<Track>();
//INSERT each Artist, including their Track references
Artists.Each(x => db.Save(x, references:true));
//SELECT all artists including their Track references
var allArtists = db.LoadSelect<Artist>();
allArtists.PrintDump();
<?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