Xamarin.iOS on real device system.typeinitializationexception using entity framework core

.net-core entity-framework-core xamarin xamarin.forms xamarin.ios

Question

Referring to this post: Xamarin iOS Linker Issue

I got the same problem. This is the exception which is thrown on this code (line is highlighted):

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

Exception:

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

I tried the suggested solution but unfortunately it didn't solve my problem.

My configuraion:

EF Core version: v2.2.1 Database Provider: Microsoft.EntityFrameworkCore.Sqlite Operating system: Windows 10 / iOS 12.1.2 IDE: Visual Studio 2017 (15.9.5)

Appreciate your help!

Edit:

This is the code of the ServiceProvider where build up the dependency injection:

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

The exception occurs on EnsureCreated().

1
0
1/21/2019 6:36:17 PM

Accepted Answer

I could resolve my problem with help of the Xamarin Support Team.

Add the following LinkDescription.xml file to your Xamarin.iOS project and set its build action to LinkDescription:

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

Source: https://github.com/xamarin/xamarin-android/issues/2620

1
1/28/2019 8:48:48 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow