using System.Data.Entity; using System.IO; using SQLite.CodeFirst.Utility; namespace SQLite.CodeFirst { /// /// An implementation of that will recreate and optionally re-seed the /// database only if the database does not exist. To seed the database, create a derived class and override the Seed method. /// /// The type of the context. public class SqliteCreateDatabaseIfNotExists : SqliteInitializerBase where TContext : DbContext { private readonly bool nullByteFileMeansNotExisting; /// /// Initializes a new instance of the class. /// /// The model builder. public SqliteCreateDatabaseIfNotExists(DbModelBuilder modelBuilder) : this(modelBuilder, false) { } /// /// Initializes a new instance of the class. /// /// The model builder. /// if set to true a null byte database file is treated like the database does not exist. public SqliteCreateDatabaseIfNotExists(DbModelBuilder modelBuilder, bool nullByteFileMeansNotExisting) : base(modelBuilder) { this.nullByteFileMeansNotExisting = nullByteFileMeansNotExisting; } /// /// Initialize the database for the given context. /// Generates the SQLite-DDL from the model and executs it against the database. /// After that the method is executed. /// All actions are be executed in transactions. /// /// The context. public override void InitializeDatabase(TContext context) { string databaseFilePath = GetDatabasePathFromContext(context); bool exists = InMemoryAwareFile.Exists(databaseFilePath, nullByteFileMeansNotExisting); if (exists) { return; } base.InitializeDatabase(context); } } }