本文介绍了Xamarin Forms + Entity Framework Core + SQLite-在生产中断应用程序上调用迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我运行以下设置

Xamarin表单-实体框架核心2.2-SQLite-安卓-调试

而且一切正常. .db文件已正确生成,并在数据库上执行了查询.然后,我准备了用于生产的应用程序,并将DEBUG更改为RELEASE编译,并打包为apk.安装在设备上时,调用Database.Migrate()Database.EnsureCreated()

First I run the following setup

Xamarin Forms - Entity Framework Core 2.2 - SQLite - Android - DEBUG

And everything works fine. The .db file is generated properly and queries are executed on the database.Then I prepared the application for production and I changed the DEBUG to RELEASE compiled, and packed into an apk. As installed on the device the application always crashes when calling either Database.Migrate() or Database.EnsureCreated()

在我尝试过的每个应用程序上,情况都是相同的.应用程序可在仿真器中以及处于调试模式的设备上正常运行.创建数据库时,应用程序在每台Android设备上崩溃.

The scenario is the same on every application I tried. App runs properly in emulator and on the device being in DEBUG mode. App crashes on every Android device when creating the database.

这是DbContext

public ItemContext(DbContextOptions<ItemContext> options)
    : base(options)
{
    //Database.Migrate();
    Database.EnsureCreated();
}

这是调用构造函数的方式

This is how the constructor is called

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));
}

这是我在Android上检索文件路径的方式.

This is how I retrieve the filepath on Android.

public string GetDatabaseFilePath(string fileName)
{
    string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
    return Path.Combine(path, fileName);
}

在Android设备监视器上查看时,显示非常长的错误开始是稍后某处列出了EnsureCreated方法

When looking on the Android Device Monitor a very long error is displayedThe beginning issomewhere later there is the EnsureCreated method listed

问题:为什么会发生这种情况,以及如何使应用程序在生产环境中可运行?

推荐答案

您是否在发行版本配置中启用了链接?根据此 https://github.com/aspnet/EntityFrameworkCore/issues/10963 编译器要求提示不要在EF Core中内部链接通过反射访问的程序集.您可以尝试切换到仅链接sdk程序集",以查看是否可以解决此问题.如果是这样,那么您将需要标识程序集并将其标记为保留.此处有更多信息: Xamarin iOS链接器问题和此处: https://docs.microsoft.com/zh-CN/xamarin/android/deploy-test/linker#linkskip .

Do you have linking enabled in your release build configuration? According to this https://github.com/aspnet/EntityFrameworkCore/issues/10963 the compiler requires hints to not link assemblies accessed through reflection internally in EF Core. You can try switching to "Link sdk assemblies only" to see if that fixes the issue. If it does then you will need to identify the assemblies and mark them to be preserved. There's some more info on that here: Xamarin iOS Linker Issue and here: https://docs.microsoft.com/en-us/xamarin/android/deploy-test/linker#linkskip.

目前我无法亲自进行测试,但我认为将[assembly: Preserve (typeof (System.Linq.Queryable), AllMembers = true)](或可能引起它的任何程序集)放入App.xaml.cs中.

I can't personally test at the moment but I think putting [assembly: Preserve (typeof (System.Linq.Queryable), AllMembers = true)] (or whatever assembly might be causing it) in your App.xaml.cs should do it.

这篇关于Xamarin Forms + Entity Framework Core + SQLite-在生产中断应用程序上调用迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 03:05