【发布时间】: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