Skip to content

Instantly share code, notes, and snippets.

@gistlyn
Last active February 1, 2022 07:40
Show Gist options
  • Select an option

  • Save gistlyn/87164fa870ac7503b43333d1d275456c to your computer and use it in GitHub Desktop.

Select an option

Save gistlyn/87164fa870ac7503b43333d1d275456c to your computer and use it in GitHub Desktop.

Revisions

  1. gistlyn revised this gist Feb 1, 2022. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions MyApp.csproj
    Original file line number Diff line number Diff line change
    @@ -2,12 +2,12 @@

    <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
    <TargetFramework>net6.0</TargetFramework>
    <NoWarn>1591</NoWarn>
    </PropertyGroup>

    <ItemGroup>
    <PackageReference Include="ServiceStack.OrmLite.Sqlite" Version="5.*" />
    <PackageReference Include="ServiceStack.OrmLite.Sqlite" Version="6.*" />
    </ItemGroup>

    </Project>
  2. gistlyn revised this gist Jan 12, 2021. No changes.
  3. gistlyn revised this gist Jan 12, 2021. No changes.
  4. gistlyn revised this gist Jan 12, 2021. 1 changed file with 0 additions and 45 deletions.
    45 changes: 0 additions & 45 deletions toc.md
    Original file line number Diff line number Diff line change
    @@ -1,45 +0,0 @@
    # <a target="_blank" href="https://github.com/ServiceStack/ServiceStack.OrmLite">OrmLite</a>

    is a fast, simple, typed code-first ORM for .NET supporting
    <a target="_blank" href="https://github.com/ServiceStack/ServiceStack.OrmLite#download">most popular RDBMS's</a>.
    For this interactive tour we'll use OrmLite's cross-platform In Memory SQLite database which
    can be referenced in Gistlyn by copying the `packages.config` NuGet generates after it installs it with:

    > Install-Package ServiceStack.OrmLite.Sqlite

    A copy of `packages.config` and test `data.cs` most examples use is available in:

    - [OrmLite Reference Test Data](#show=data.cs)

    > Tip: quickly browse source code by navigating tabs with `Ctrl` + `Left` and `Right` arrow keys and use `Ctrl` + `Enter` to run Gists - see `?` for more shortcuts
    For a quick 1-page dive into OrmLite, checkout:

    - [OrmLite CRUD demo](/a62b1e7a8da57c7d1fda95b3da5303eb) ([async](/f01776346da21fddbee0063703a810f8))
    - [OrmLite Rich CREATE Table Example](/840bc7f09292ad5753d07cef6063893e)

    ### Try out features

    See each section for more in-depth examples of OrmLite's features:

    - [SELECT Examples](/cd381848f252be2a84f8c239ed0d241b) - Typed, Advanced, Custom and dynamic Queries with OrmLite
    - [INSERT Examples](/cd125db90e8bde1aa41c3ee874463909) - Inserting records
    - [UPDATE Examples](/849680f095fb2721b2714d0bbcddc8d7) - Updating records
    - [DELETE Examples](/614c92388fbcad67d93ce3995198d85d) - Deleting records
    - [CREATE Table Examples](/1cf99946dd378c7d7ad678d052c29d38) - Creating Table Schemas for POCO Type definitions
    - [Modify Schema Examples](/c2e802610cfbfc5e9ba4e2856d6535e0) - Modifying existing tables
    - [Other Features](/696e8ba9602665f678855eb34c775b84)
    - Async APIs
    - Transaction Support
    - Optimistic Concurrency
    - Multitenancy
    - Global Insert / Update Filters
    - Exec, Result and String Filters
    - Mocking
    - Pluggable Complex Type Serializers
    - Custom Configuration

    ---

    ### [< Home](/2cc6b5db6afd3ccb0d0149e55fdb3a6a) · [SELECT Examples >](/cd381848f252be2a84f8c239ed0d241b)

  5. gistlyn renamed this gist Jan 8, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. gistlyn revised this gist Jan 8, 2021. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion main.cs
    Original file line number Diff line number Diff line change
    @@ -18,4 +18,6 @@

    //SELECT all artists including their Track references
    var allArtists = db.LoadSelect<Artist>();
    allArtists.PrintDump();
    allArtists.PrintDump();

    Inspect.vars(new { allArtists });
  7. gistlyn revised this gist Jan 8, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion main.cs
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@
    db.DropAndCreateTable<Track>();

    //INSERT each Artist, including their Track references
    Artists.Each(x => db.Save(x, references:true));
    Seed.Artists.Each(x => db.Save(x, references:true));

    //SELECT all artists including their Track references
    var allArtists = db.LoadSelect<Artist>();
  8. gistlyn created this gist Jan 8, 2021.
    13 changes: 13 additions & 0 deletions MyApp.csproj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    <Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
    <NoWarn>1591</NoWarn>
    </PropertyGroup>

    <ItemGroup>
    <PackageReference Include="ServiceStack.OrmLite.Sqlite" Version="5.*" />
    </ItemGroup>

    </Project>
    68 changes: 68 additions & 0 deletions data.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    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;
    }

    public static class Seed
    {
    public static IDbConnection CreateArtistAndTrackTablesWithData(IDbConnection db)
    {
    db.DropAndCreateTable<Artist>();
    db.DropAndCreateTable<Track>();
    Artists.Each(x => db.Save(x, references:true));
    return db;
    }

    public static Artist[] 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 },
    }
    },
    };
    }
    45 changes: 45 additions & 0 deletions index.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    # <a target="_blank" href="https://github.com/ServiceStack/ServiceStack.OrmLite">OrmLite</a>

    is a fast, simple, typed code-first ORM for .NET supporting
    <a target="_blank" href="https://github.com/ServiceStack/ServiceStack.OrmLite#download">most popular RDBMS's</a>.
    For this interactive tour we'll use OrmLite's cross-platform In Memory SQLite database which
    can be referenced in Gistlyn by copying the `packages.config` NuGet generates after it installs it with:

    > Install-Package ServiceStack.OrmLite.Sqlite

    A copy of `packages.config` and test `data.cs` most examples use is available in:

    - [OrmLite Reference Test Data](#show=data.cs)

    > Tip: quickly browse source code by navigating tabs with `Ctrl` + `Left` and `Right` arrow keys and use `Ctrl` + `Enter` to run Gists - see `?` for more shortcuts
    For a quick 1-page dive into OrmLite, checkout:

    - [OrmLite CRUD demo](/a62b1e7a8da57c7d1fda95b3da5303eb) ([async](/f01776346da21fddbee0063703a810f8))
    - [OrmLite Rich CREATE Table Example](/840bc7f09292ad5753d07cef6063893e)

    ### Try out features

    See each section for more in-depth examples of OrmLite's features:

    - [SELECT Examples](/cd381848f252be2a84f8c239ed0d241b) - Typed, Advanced, Custom and dynamic Queries with OrmLite
    - [INSERT Examples](/cd125db90e8bde1aa41c3ee874463909) - Inserting records
    - [UPDATE Examples](/849680f095fb2721b2714d0bbcddc8d7) - Updating records
    - [DELETE Examples](/614c92388fbcad67d93ce3995198d85d) - Deleting records
    - [CREATE Table Examples](/1cf99946dd378c7d7ad678d052c29d38) - Creating Table Schemas for POCO Type definitions
    - [Modify Schema Examples](/c2e802610cfbfc5e9ba4e2856d6535e0) - Modifying existing tables
    - [Other Features](/696e8ba9602665f678855eb34c775b84)
    - Async APIs
    - Transaction Support
    - Optimistic Concurrency
    - Multitenancy
    - Global Insert / Update Filters
    - Exec, Result and String Filters
    - Mocking
    - Pluggable Complex Type Serializers
    - Custom Configuration

    ---

    ### [< Home](/2cc6b5db6afd3ccb0d0149e55fdb3a6a) · [SELECT Examples >](/cd381848f252be2a84f8c239ed0d241b)

    21 changes: 21 additions & 0 deletions main.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    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();