Skip to content

Instantly share code, notes, and snippets.

@gistlyn
Created June 30, 2021 07:12
Show Gist options
  • Save gistlyn/d2e4cae3e23848ea9683edef9b86a9f7 to your computer and use it in GitHub Desktop.
Save gistlyn/d2e4cae3e23848ea9683edef9b86a9f7 to your computer and use it in GitHub Desktop.
data-access
public class Author
{
public Author(){}
[AutoIncrement]
[Alias("AuthorID")]
public Int32 Id { get; set;}
[Index(Unique = true)]
[StringLength(40)]
public string Name { get; set;}
public DateTime Birthday { get; set;}
public DateTime ? LastActivity { get; set;}
public Decimal? Earnings { get; set;}
public bool Active { get; set; }
[StringLength(80)]
[Alias("JobCity")]
public string City { get; set;}
[StringLength(80)]
[Alias("Comment")]
public string Comments { get; set;}
public Int16 Rate { get; set;}
}
var dbFactory = HostContext.Resolve<IDbConnectionFactory>();
using var db = dbFactory.OpenDbConnection();
int agesAgo = DateTime.Today.AddYears(-20).Year;
db.Select<Author>(x => x.Birthday >= new DateTime(agesAgo, 1, 1)
&& x.Birthday <= new DateTime(agesAgo, 12, 31));
db.Select<Author>(x => Sql.In(x.City, "London", "Madrid", "Berlin"));
db.Select<Author>(x => x.Earnings <= 50);
db.Select<Author>(x => x.Name.StartsWith("A"));
db.Select<Author>(x => x.Name.EndsWith("garzon"));
db.Select<Author>(x => x.Name.Contains("Benedict"));
//impicit string casting
db.Select<Author>(x => x.Rate.ToString() == "10");
//server string concatenation
db.Select<Author>(x => "Rate " + x.Rate == "Rate 10");
public class AppHost : AppHostBase
{
public AppHost() : base("Web",typeof(MyServices).Assembly){}
public override void Configure(Container container)
{
var connectionString =
"Server=127.0.0.1;Port=5432;" +
"Database=host;User Id=user;" +
"Password=myPassword;";
container.Register<IDbConnectionFactory>(
new OrmLiteConnectionFactory(
connectionString,
PostgreSqlDialect.Provider));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment