Error in Database.EnsureCreated() while using Enti

2019-08-26 07:54发布

问题:

I'm using Entity Framework in Xamarin Forms that uses sqlite. When I debug Xamarin.Android, everything is ok, also in debugging Xamarin.iOs with iPhone simulator, it works fine.

My DbContext is:

public class DB:DbContext
{
    // Tables
    private readonly string _databasepath;

    public DB(string databasepath)
    {
        _databasepath = databasepath;
        Database.EnsureCreated();
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlite($"filename={_databasepath}");
    }
}

But when I run my project on an iOS device, this exception will thrown at Database.EnsureCreated():

The type initializer for 'Microsoft.EntityFrameworkCore.Query.ResultOperators.Internal.TrackingExpressionNode' threw an exception.

What's wrong?