Skip to content

Instantly share code, notes, and snippets.

@mythz
Last active May 9, 2017 05:49
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/41382a607a946e647e1960b664f729a3 to your computer and use it in GitHub Desktop.
Save mythz/41382a607a946e647e1960b664f729a3 to your computer and use it in GitHub Desktop.
Image Place demo
using System;
using System.Collections.Generic;
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
[Alias("Places")]
public class Place
{
public Guid Id { get; set; }
[Reference]
public List<Image> Countries { get; set; }
}
[Alias("Images")]
public class Image
{
public Guid Id { get; set; }
//[Alias("ColumnAliasIfNotPlaceId")]
public Guid PlaceId { get; set; }
public string Url { get; set; } //Example field on Image
}
db.CreateTable<Place>();
db.CreateTable<Image>();
var placeId = Guid.NewGuid();
var place = new Place
{
Id = placeId,
Countries = new List<Image> {
new Image { Id = Guid.NewGuid(), PlaceId = placeId, Url = "http://url.to/country1.png" },
new Image { Id = Guid.NewGuid(), PlaceId = placeId, Url = "http://url.to/country2.png" },
}
};
db.Save(place, references: true);
var fromDb = db.LoadSingleById<Place>(placeId);
fromDb.PrintDump();
"Images Only:\n{0}".Print(db.Select<Image>(x => x.PlaceId == placeId).Dump());
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="ServiceStack.Text" version="4.5.8" targetFramework="net45" />
<package id="ServiceStack.Interfaces" version="4.5.8" targetFramework="net45" />
<package id="ServiceStack.Common" version="4.5.8" targetFramework="net45" />
<package id="ServiceStack.OrmLite" version="4.5.8" targetFramework="net45" />
<package id="ServiceStack.OrmLite.Sqlite.Mono" version="4.5.8" targetFramework="net45" />
</packages>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment