【问题标题】:Xamarin.Forms Sqlite-net NotSupportedException on ManyToOne relationship "Don't know about <model>"Xamarin.Forms Sqlite-net NotSupportedException on ManyToOne 关系“不知道 <model>”
【发布时间】:2017-05-30 19:30:46
【问题描述】:

我在我的 xamarin.forms 应用程序中使用 sqlite 网络扩展库。我在我的 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
}

【问题讨论】:

  • 您只将一个参数传递给 SQLiteConnection 构造函数,因此我不得不假设您使用 not 使用 Sqlite.Net.SQLiteConnection,这需要 ISQLitePlatform 对象作为第一个参数因此SQLiteNetExtensions.Attributes.ManyToOneAttribute 不会被SQLiteConnection 知道。
  • @SushiHangover 我不知道有两种类型的同名 SQLiteConnection。这段代码在我的 PCL 中,所以我必须实现一些东西来获取每个平台 ISQLitePlatform 的实例吗?另外你知道实现 ISQLitePlatform 的类叫什么吗?
  • SQLite 扩展提供了三种风格,不确定您使用的是哪一种,我会根据您使用的风格点击链接并查看集成测试...bitbucket.org/twincoders/sqlite-net-extensions/src/…

标签: c# sqlite xamarin xamarin.forms


【解决方案1】:

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

我需要使用 Sqlite.Net.SQLiteConnection 类来进行 sqlite 网络扩展。不是我使用的Sqlite.SQLiteConnection

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

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

public interface ISQLitePlatformInstance
{
    ISQLitePlatform GetSQLitePlatformInstance();
}

我在我的 Droid 项目(我正在构建的平台)中实现了接口:

public class SQLitePlatformInstance : ISQLitePlatformInstance
{

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-20
    • 2023-02-03
    • 1970-01-01
    相关资源
    最近更新 更多