I'm new to Dapper and currently I'm trying to populate my fresh database. Since Dapper has no ability to create a table from model directly, how should I populate my database? Are there any good practices other than executing a schema from the variable?
Currently, I'm using this (dirty approach):
public void CreateTables()
{
using (var connection = GetSQLiteHandle())
{
string sql = @"
CREATE TABLE IF NOT EXISTS statistics (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
name TEXT NOT NULL,
value TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS posts (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
content TEXT NOT NULL,
created TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS names (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
name TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
username TEXT NOT NULL,
email TEXT NOT NULL,
created TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS storage (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
key TEXT NOT NULL,
value TEXT NOT NULL
);
";
connection.Execute(sql);
}
}
Dapper has no ability to generate tables like Entity Framework. Hence, you may use EF with code first approach. If you want to use Dapper, you can use migrator tools such as fluent migrator and generate db with it.