Skip to content

Instantly share code, notes, and snippets.

@tarasn
Created August 2, 2017 21:47
Show Gist options
  • Save tarasn/80789efe1c3c78a2d6f2bba6dee8d617 to your computer and use it in GitHub Desktop.
Save tarasn/80789efe1c3c78a2d6f2bba6dee8d617 to your computer and use it in GitHub Desktop.
NPoco with sqlite using mapping by convention
using System;
using System.Data.SQLite;
using NPoco;
using NPoco.FluentMappings;
namespace NpocoSqlite
{
/*
CREATE TABLE [TestMe](
[MySuperID] INTEGER PRIMARY KEY NOT NULL,
[TextData] TEXT NOT NULL);
*/
public class CategoryMapping : Map<Category>
{
public CategoryMapping()
{
PrimaryKey(x => x.CategoryId, false);
TableName("TestMe");
Columns(x =>
{
x.Column(y => y.Text).WithName("TextData");
x.Column(y => y.CategoryId).WithName("MySuperID");
});
}
}
public static class MyFactory
{
public static DatabaseFactory DbFactory { get; set; }
public static void Setup()
{
var fluentConfig = FluentMappingConfiguration.Configure(new CategoryMapping());
//or individual mappings
//var fluentConfig = FluentMappingConfiguration.Configure(new CategoryMapping(), ....);
DbFactory = DatabaseFactory.Config(x =>
{
var conn = new SQLiteConnection(@"Data Source=C:\Experiment Space\csharp\NpocoSqlite\storage.sqlite");
x.UsingDatabase(() => new Database(conn, DatabaseType.SQLite));
x.WithFluentConfig(fluentConfig);
//x.WithMapper(new Mapper());
conn.Open();
});
}
}
//[TableName("TestMe")]
// [PrimaryKey("MySupperID", AutoIncrement = false)]
public class Category
{
//[Column("MySupperID")]
public int CategoryId { get; set; }
// [Column("TextData")]
public string Text { get; set; }
}
internal class Program
{
private static void Main(string[] args)
{
MyFactory.Setup();
var db = MyFactory.DbFactory.GetDatabase();
db.Save(new Category
{
CategoryId = 88,
Text = "dsfdsfds546565465465fdsfdsf"
});
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment