Last active
November 10, 2020 13:32
-
-
Save gistlyn/f01776346da21fddbee0063703a810f8 to your computer and use it in GitHub Desktop.
Simple OrmLite CRUD demo (async)
This file contains hidden or 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 System; | |
| using System.Collections.Generic; | |
| using ServiceStack; | |
| using ServiceStack.Text; | |
| using ServiceStack.OrmLite; | |
| using ServiceStack.OrmLite.Sqlite; | |
| using ServiceStack.DataAnnotations; | |
| /*** OrmLite Async: | |
| * Most of OrmLite's public APIs have async equivalents of the same name with an | |
| * additional conventional `*Async` suffix as per .NET Guidelines. | |
| * Async APIs also take an optional CancellationToken simplifying, converting sync | |
| * code by just adding the `*Async` suffix and `await` keyword as seen below: | |
| ***/ | |
| public class User | |
| { | |
| public long Id { get; set; } | |
| [Index] | |
| public string Name { get; set; } | |
| public DateTime CreatedDate { get; set; } | |
| public override string ToString() => Name; | |
| } | |
| var dbFactory = new OrmLiteConnectionFactory(":memory:", SqliteDialect.Provider); | |
| var db = dbFactory.Open(); //Open ADO.NET DB Connection | |
| db.DropAndCreateTable<User>(); //DROP (if exist) and CREATE Table from User POCO | |
| await db.InsertAsync( //INSERT multiple Users by params | |
| new User { Id = 1, Name = "A", CreatedDate = DateTime.Now }, | |
| new User { Id = 2, Name = "B", CreatedDate = DateTime.Now }, | |
| new User { Id = 3, Name = "C", CreatedDate = DateTime.Now }, | |
| new User { Id = 4, Name = "C", CreatedDate = DateTime.Now }); | |
| var rowsC = await db.SelectAsync<User>(x => x.Name == "C"); //SELECT by typed expression | |
| $"No of 'C' Rows: {rowsC.Count}, Ids:".Print(); //= 2 | |
| rowsC.ConvertAll(x => x.Id).PrintDump(); //= 3,4 | |
| await db.DeleteAsync<User>(x => x.Name == "C"); //DELETE by typed expression | |
| var remainingC = await db.SelectAsync<User>("Name= @name", new { name="C" }); //Custom SQL | |
| $"No of 'C' Rows: {remainingC.Count}".Print(); //= 0 | |
| var rowsLeft = await db.SelectAsync<User>(); | |
| $"Rows Left: {rowsLeft.Count}".Print(); //= 2 | |
| rowsLeft.PrintDump(); //= A,B |
This file contains hidden or 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="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