Reputation: 21
I am writing a small program to test out linq2db. What I want to do is create the database if it does not already exist. Most(all?) of the linq2db documentation assumes an existing database. I check if the database exists and if not create an empty database using CreateCommand as below. The Intellisense for this says this method is "For internal use only". Is it OK to use CreateCommand or is there a better way?
namespace SqliteTest
{
public partial class ImageDatabaseDB : LinqToDB.Data.DataConnection
{
public ITable<testTable> testTables { get { return this.GetTable<testTable>(); } }
public void InitMappingSchema()
{
}
public ImageDatabaseDB()
{
InitDataContext();
InitMappingSchema();
}
public ImageDatabaseDB(string configuration) : base(configuration)
{
InitDataContext();
InitMappingSchema();
}
partial void InitDataContext();
}
[Table("testTable")]
public partial class testTable
{
[PrimaryKey, Identity] public int id;
[Column, NotNull] public string Name;
[Column, Nullable] public string Description;
}
public static partial class TableExtensions
{
public static testTable Find(this ITable<testTable> table, long id)
{
return table.FirstOrDefault(t =>
t.id == id);
}
}
class Program
{
static void Main(string[] args)
{
const string dbLocation = @".\TestDatabase\TestDatabase.sqlite";
if (!File.Exists(dbLocation))
{
using (var db = new TestDatabaseDB())
{
db.CreateCommand();
// create tables...
}
}
}
Upvotes: 1
Views: 4271
Reputation:
To create sqlite database you should use LinqToDB.DataProvider.SQLite.SQLiteTools.CreateDatabase(...)
method
Upvotes: 2
Reputation: 2970
Linq2db offers Database First approach only, creating DB schema from entity mappings (Code First approach) is not supported out-of-the-box.
However, Linq2db is able to generate CREATE TABLE DDL statements from mappings by IDataContext.CreateTable<T>. See also this discussion.
Upvotes: 3