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().
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