Make a backup before the database is dropped DropCreateDatabaseIfModelChanges
1 answer
We will implement the interface shortly IDatabaseInitializer<T>
.
So far we are using this code (which I know is not perfect):
Database.SetInitializer<OurdatabaseModel>(new OurdatabaseInitializer());
_instance = new OurdatabaseModel();
try
{
// force model creation
_instance.Database.Initialize(false);
}
catch (InvalidOperationException)
{
if (_instance == null)
{
throw;
}
// database exists. Let back it up.
string dbPath = _instance.Database.Connection.Database.Replace("|DataDirectory|", Program.DataDirectory);
File.Move(dbPath, dbPath + "." + DateTime.Now.ToString("yyyy-MM-dd_hh-mm-ss") + ".sdf");
// and now the CreateDatabaseIfNotExists<T> will take care of the rest
_instance = new OurdatabaseModel();
}
0
source to share