【问题标题】:Xamarin.iOS on real device system.typeinitializationexception using entity framework core真实设备上的 Xamarin.iOS system.typeinitializationexception 使用实体框架核心
【发布时间】:2019-01-17 22:37:37
【问题描述】:

参考此帖:Xamarin iOS Linker Issue

我遇到了同样的问题。这是此代码引发的异常(突出显示的行):

using (var scope = ServiceProvider.CreateScope())
{
  var dbContextLocal = 
        scope.ServiceProvider.GetRequiredService<C4S_DataContext>();
  dbContextLocal.Database.EnsureCreated(); //Exception occurs here

  if (!dbContextLocal.Kontakte.Any())
  {
      var settingsViewModel = new SettingsViewModel(dbContextLocal);
      settingsViewModel.SyncDatabasesCommand.Execute(null);
  }
}

例外:

system.typeinitializationexception: The type initializer for 'Microsoft.EntityFrameworkCore.Sqlite.Query.ExpressionTranslators.Internal.Sql iteCompositeMethodCallTranslator' threw an exception.

我尝试了建议的解决方案,但不幸的是它没有解决我的问题。

我的配置:

EF Core 版本:v2.2.1 数据库提供程序:Microsoft.EntityFrameworkCore.Sqlite 操作系统:Windows 10 / iOS 12.1.2 IDE:Visual Studio 2017 (15.9.5)

感谢您的帮助!

编辑:

这是建立依赖注入的 ServiceProvider 的代码:

        //Services für Dependency Injection 
    public void RegisterServices(IServiceCollection services)
    {
        // Register services here.
        services.AddEntityFrameworkSqlite();

        services.AddDbContext<C4S_DataContext>(options =>
    options.UseSqlite($"Filename={_dbPath}"));

        services.AddTransient<PortfolioGruppenViewModel>();
        services.AddTransient<PortfolioElementeViewModel>();
        services.AddTransient<RegionenViewModel>();
        services.AddTransient<KontakteGroupViewModel>();
        services.AddTransient<KontaktDetailsViewModel>();

        services.AddTransient<SettingsViewModel>();

        ServiceProvider = services.BuildServiceProvider();

        using (var scope = ServiceProvider.CreateScope())
        {
            var dbContextLocal = scope.ServiceProvider.GetRequiredService<C4S_DataContext>();
            dbContextLocal.Database.EnsureCreated();

            if (!dbContextLocal.Kontakte.Any())
            {
                var settingsViewModel = new SettingsViewModel(dbContextLocal);
                settingsViewModel.SyncDatabasesCommand.Execute(null);
            }
        }
    }

EnsureCreated() 发生异常。

【问题讨论】:

  • 你能提供更多关于你的项目的代码吗?比如ServiceProvider的代码。如果您提供一个简单的演示来重现此问题会更好。
  • 请看我的编辑。希望这是您要求的正确代码。

标签: xamarin xamarin.forms xamarin.ios .net-core entity-framework-core


【解决方案1】:

我可以在 Xamarin 支持团队的帮助下解决我的问题。

将以下 LinkDescription.xml 文件添加到 Xamarin.iOS 项目并将其生成操作设置为 LinkDescription:

<?xml version="1.0" encoding="UTF-8" ?>
<linker>
    <assembly fullname="mscorlib">
         <type fullname="System.DateTime" preserve="methods" />
    </assembly>
</linker>

来源:https://github.com/xamarin/xamarin-android/issues/2620

【讨论】:

    猜你喜欢
    • 2021-07-14
    • 1970-01-01
    • 1970-01-01
    • 2018-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-03
    相关资源
    最近更新 更多