【发布时间】:2019-01-17 22:37:37
【问题描述】:
我遇到了同样的问题。这是此代码引发的异常(突出显示的行):
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