Xamarin 上带有 SQLite 的 EF Core:如何删除数据库并通过代码重新运行迁移
EF Core with SQLite on xamarin: how to delete database and rerun migrations by code
我的应用程序可以重置数据库,这样来自服务器的同步将再次下载所有数据。在使用 EF Core 之前,我只是删除数据库文件并重新创建它,现在,使用 EF Core,我不确定该怎么做,如果我执行 EnsureDeleted,之后 .Migrate() 将不起作用。如果我手动删除数据库文件,.Migrate() 也不会工作...
所以,我试过这个:
using (var model = new XSModel(false, false))
{
model.Database.EnsureDeleted();
}
using (var model = new XSModel(false, false))
{
model.Database.Migrate();
}
迁移抛出:
Microsoft.Data.Sqlite.SqliteException: 'SQLite Error 14: 'unable to open database file'.'
asdfsadfas
我试过这个:
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();
}
并且随着这个迁移也会抛出:
Microsoft.Data.Sqlite.SqliteException: 'SQLite Error 14: 'unable to open database file'.'
我现在在 EF Core 3.x 中的做法是这样的:
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;
}
我的应用程序可以重置数据库,这样来自服务器的同步将再次下载所有数据。在使用 EF Core 之前,我只是删除数据库文件并重新创建它,现在,使用 EF Core,我不确定该怎么做,如果我执行 EnsureDeleted,之后 .Migrate() 将不起作用。如果我手动删除数据库文件,.Migrate() 也不会工作...
所以,我试过这个:
using (var model = new XSModel(false, false))
{
model.Database.EnsureDeleted();
}
using (var model = new XSModel(false, false))
{
model.Database.Migrate();
}
迁移抛出:
Microsoft.Data.Sqlite.SqliteException: 'SQLite Error 14: 'unable to open database file'.'
asdfsadfas
我试过这个:
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();
}
并且随着这个迁移也会抛出:
Microsoft.Data.Sqlite.SqliteException: 'SQLite Error 14: 'unable to open database file'.'
我现在在 EF Core 3.x 中的做法是这样的:
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;
}