Created
April 25, 2016 19:16
-
-
Save anonymous/0e11f74f075231f5c5be3dcc0dd7510b to your computer and use it in GitHub Desktop.
This file contains 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.Data.Common; | |
using System.Data.SQLite; | |
using Dapper; | |
using FluentMigrator; | |
using System.Linq; | |
using System.Reflection; | |
using FluentMigrator.Runner; | |
using FluentMigrator.Runner.Announcers; | |
using FluentMigrator.Runner.Initialization; | |
namespace SqliteDapperFluentMigrator | |
{ | |
class Program | |
{ | |
const string ConnectionString = "Data Source=database.sqlite;Version=3"; | |
static DbConnection connection; | |
static MigrationRunner runner; | |
static void Main(string[] args) | |
{ | |
var table = new PostTable(); | |
CreateDatabase(); | |
runner.Up(table); | |
QueryAllAndFilterWithLinq(); | |
Console.ReadKey(); | |
QueryAndFilterDatabaseSide(); | |
runner.Down(table); | |
Console.ReadKey(); | |
} | |
static void QueryAndFilterDatabaseSide() | |
{ | |
Console.WriteLine(nameof(QueryAndFilterDatabaseSide)); | |
var posts = connection.Query<Post>("SELECT * FROM Post WHERE Posted = @date", new { date = DateTime.Today }); | |
Console.WriteLine($"{posts.Count()} posts found"); | |
Console.WriteLine($"Found Post: {posts?.FirstOrDefault()?.ToString() ?? "No record found"}"); | |
} | |
static void QueryAllAndFilterWithLinq() | |
{ | |
Console.WriteLine(nameof(QueryAllAndFilterWithLinq)); | |
var posts = connection.Query<Post>("SELECT * FROM Post"); | |
Console.WriteLine($"{posts.Count()} posts found"); | |
var filteredPost = posts.FirstOrDefault(it => it.Posted == DateTime.Today); | |
Console.WriteLine($"Found Post: {filteredPost}"); | |
} | |
static void CreateDatabase() | |
{ | |
connection = new SQLiteConnection(ConnectionString); | |
runner = CreateRunner(); | |
} | |
static MigrationRunner CreateRunner() | |
{ | |
var announcer = new TextWriterAnnouncer(Console.WriteLine); | |
var assembly = Assembly.GetExecutingAssembly(); | |
var context = new RunnerContext(announcer) | |
{ | |
Namespace = typeof(Program).Namespace, | |
Connection = ConnectionString | |
}; | |
var options = new MigrationOptions(); | |
var factory = new FluentMigrator.Runner.Processors.SQLite.SQLiteProcessorFactory(); | |
return new MigrationRunner(assembly, context, factory.Create(ConnectionString, announcer, options)); | |
} | |
class MigrationOptions : IMigrationProcessorOptions | |
{ | |
public bool PreviewOnly => false; | |
public string ProviderSwitches | |
{ | |
get; set; | |
} | |
public int Timeout => 60; | |
} | |
class Post | |
{ | |
internal long Id { get; set; } | |
internal string Title { get; set; } | |
internal DateTime Posted { get; set; } | |
public override string ToString() => $"Id: {Id} Title: {Title} Posted: {Posted}"; | |
} | |
[Migration(1, "Blog")] | |
class PostTable : Migration | |
{ | |
public override void Up() | |
{ | |
Create | |
.Table("Post") | |
.WithColumn("Id").AsInt32().PrimaryKey().NotNullable() | |
.WithColumn("Title").AsString(255).NotNullable() | |
.WithColumn("Posted").AsDateTime().NotNullable(); | |
Insert | |
.IntoTable("Post") | |
.Row(new { Id = 1, Title = "My first Post", Posted = DateTime.Today }) | |
.Row(new { Id = 2, Title = "My second Post", Posted = DateTime.Today.AddDays(1) }); | |
} | |
public override void Down() | |
{ | |
Delete.Table("Post"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment