it's possible in my app to reset the database so the synchronization from the server will download all the data again. Before using EF Core, I would just delete the database file and recreate it, now, with EF Core, I'm not sure what to do, if I do a EnsureDeleted, the .Migrate() doesn't work after. And if I manually delete the database file, the .Migrate() also won't work...
So, I have tried this:
using (var model = new XSModel(false, false))
{
model.Database.EnsureDeleted();
}
using (var model = new XSModel(false, false))
{
model.Database.Migrate();
}
Migrate throws:
Microsoft.Data.Sqlite.SqliteException: 'SQLite Error 14: 'unable to open database file'.'
asdfsadfas
I have tried this:
var filePath = new Java.IO.File(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "dbname.db"));
if (filePath.Exists())
filePath.Delete();
using (var model = new XSModel(false, false))
{
model.Database.Migrate();
}
And with this migrate also throws:
Microsoft.Data.Sqlite.SqliteException: 'SQLite Error 14: 'unable to open database file'.'
The way I'm doing it now in EF Core 3.x is this:
public bool UpdateDatabase(bool ensureDeleted)
{
var success = true;
if (ensureDeleted)
{
using (var unitOfWork = this.CreateScope(false, false))
{
unitOfWork.EnsureDeleted();
unitOfWork.DbContext.GetDependencies().StateManager.ResetState();
this.ContextDataSettings.IsFirstInitializationCompleted = false;
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
try
{
using (var unitOfWork = this.CreateScope(false, false))
success = unitOfWork.MigrateDatabase();
}
catch (Exception e)
{
if (!ensureDeleted)
{
this.UpdateDatabase(true);
return success;
}
this._logger.Error(e, "Could not migrate");
throw;
}
return success;
}