首先,我运行以下设置
Xamarin Forms - Entity Framework Core 2.2 - SQLite - Android - DEBUG
一切正常。.db 文件正确生成,查询在数据库上执行。然后我准备了用于生产的应用程序,我将DEBUG更改为RELEASE编译,并打包成一个apk。安装在设备上时,应用程序总是在调用Database.Migrate()或调用时崩溃Database.EnsureCreated()
我尝试过的每个应用程序的场景都是一样的。应用程序在模拟器和处于调试模式的设备上正常运行。创建数据库时,应用程序在每个 Android 设备上崩溃。
这是实例化 DbContext
public ItemContext(DbContextOptions<ItemContext> options)
: base(options)
{
//Database.Migrate();
Database.EnsureCreated();
}
Run Code Online (Sandbox Code Playgroud)
这就是构造函数的调用方式
public void Load()
{
string databasePath = DependencyService.Get<ILocalFileStorageService>().GetDatabaseFilePath("ItemSQLite.db");
string connectionString = $"Filename={databasePath}";
DbContextOptions<ItemContext> options = new DbContextOptionsBuilder<ItemContext>()
.UseSqlite(connectionString)
.Options;
dataService = new DataService(new ItemContext(options));
}
Run Code Online (Sandbox Code Playgroud)
这就是我在 Android 上检索文件路径的方式。
public string GetDatabaseFilePath(string fileName)
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
return Path.Combine(path, fileName);
}
Run Code Online (Sandbox Code Playgroud)
c# sqlite production-environment xamarin.forms entity-framework-core