关于ManyToOne关系的Xamarin.Forms Sqlite-net NotSupportedException&#34;不知道<model>&#34;

时间:2017-01-16 01:30:14

标签: c# sqlite xamarin xamarin.forms

我在我的xamarin.forms应用程序中使用sqlite net扩展库。我在PCL中编写了数据库代码和模型。

当我调用SQLiteConnection.CreateTable()时出现错误 System.NotSupportedException: Don't know about Cigars.Models.Cigar

Smoke是Cigar的孩子,它有ManyToOne关系。 以下是模型:

public class Smoke
{

    [PrimaryKey]
    public int SmokeId { get; set; }

    [ForeignKey(typeof(Cigar))]
    public int CigarId { get; set; }

    public string Notes { get; set; }

    public DateTime DateCreated { get; set; }

    //why is this not recognized?
    [ManyToOne]
    public Cigar Cigar { get; set; }

}

雪茄

public class Cigar
{

    [PrimaryKey]
    public int CigarId { get; set; }

    public string Name { get; set; }

    public double Length { get; set; }
}

我的数据库调用会导致抛出异常:

private SQLiteConnection db;

public Database(string path)
{
    db = new SQLiteConnection(path);
    db.CreateTable<Cigar>();
    db.CreateTable<Smoke>(); //this throws the error
}

1 个答案:

答案 0 :(得分:2)

问题是我为sqlite安装了两个不同的库,两个库都包含SQLiteConnection

我需要将Sqlite.Net.SQLiteConnection类用于sqlite net扩展。不是我使用的Sqlite.SQLiteConnection

正如SushiHangover所指出的,这个Sqlite.Net.SQLiteConnection采用ISQLitePlatform类型的第一个参数。

为了获得对此的引用,我定义了一个接口,该接口提供了返回ISQLitePlatform的方法的签名:

public interface ISQLitePlatformInstance
{
    ISQLitePlatform GetSQLitePlatformInstance();
}

我在我的Droid项目中实现了界面(我反对的平台):

public class SQLitePlatformInstance : ISQLitePlatformInstance
{

    public ISQLitePlatform GetSQLitePlatformInstance()
    {
        return new SQLitePlatformAndroid();
    }

}