using System.Data.Entity;
using System.IO;
using SQLite.CodeFirst.Utility;
namespace SQLite.CodeFirst
{
///
/// An implementation of that will always recreate and optionally re-seed the
/// database the first time that a context is used in the app domain. To seed the database, create a derived class and override the Seed method.
///
/// The type of the context.
public class SqliteDropCreateDatabaseAlways : SqliteInitializerBase
where TContext : DbContext
{
///
/// Initializes a new instance of the class.
///
/// The model builder.
public SqliteDropCreateDatabaseAlways(DbModelBuilder modelBuilder)
: base(modelBuilder)
{ }
///
/// 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 databseFilePath = GetDatabasePathFromContext(context);
bool exists = InMemoryAwareFile.Exists(databseFilePath);
if (exists)
{
FileAttributes? attributes = InMemoryAwareFile.GetFileAttributes(databseFilePath);
InMemoryAwareFile.Delete(databseFilePath);
base.InitializeDatabase(context);
InMemoryAwareFile.SetFileAttributes(databseFilePath, attributes);
}
else
{
base.InitializeDatabase(context);
}
}
}
}