Xamarin.Forms在ManyToOne关系上的Sqlite-net NotSupportedException“不知道< model>" [英] Xamarin.Forms Sqlite-net NotSupportedException on ManyToOne relationship "Don't know about <model>"
问题描述
我在xamarin.forms应用程序中使用sqlite net扩展库.我在PCL中编写数据库代码和模型.
I am using sqlite net extensions library in my xamarin.forms application. I code in my PCL the database code and models.
当我调用SQLiteConnection.CreateTable()时出现错误
System.NotSupportedException: Don't know about Cigars.Models.Cigar
When I call SQLiteConnection.CreateTable() I get an error
System.NotSupportedException: Don't know about Cigars.Models.Cigar
烟是雪茄的孩子,它具有ManyToOne关系. 这是模型:
Smoke is a child of Cigar, it has a ManyToOne relationship. Here are the models:
抽烟
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; }
}
我的引发异常的数据库调用:
My database call that causes the exception to be thrown:
private SQLiteConnection db;
public Database(string path)
{
db = new SQLiteConnection(path);
db.CreateTable<Cigar>();
db.CreateTable<Smoke>(); //this throws the error
}
推荐答案
问题是我为sqlite安装了两个不同的库,都包含SQLiteConnection
.
The problem was that I had installed two different libraries for sqlite, both containing a SQLiteConnection
.
我需要将Sqlite.Net.SQLiteConnection
类用于sqlite网络扩展.不是我正在使用的Sqlite.SQLiteConnection
.
I needed to use the Sqlite.Net.SQLiteConnection
class for sqlite net extensions. Not the Sqlite.SQLiteConnection
I was using.
如SushiHangover所指出的,此Sqlite.Net.SQLiteConnection
接受类型为ISQLitePlatform
的第一个参数.
As SushiHangover pointed out, this Sqlite.Net.SQLiteConnection
takes a first argument of type ISQLitePlatform
.
为此,我定义了一个接口,该接口提供了返回ISQLitePlatform
的方法的签名:
To obtain a reference for this, I defined an interface that provides the signature of a method to return a ISQLitePlatform
:
public interface ISQLitePlatformInstance
{
ISQLitePlatform GetSQLitePlatformInstance();
}
然后在我的Droid项目(我要建立的平台)中实现了该接口:
And I implemented the interface in my Droid project ( the platform I'm building against):
public class SQLitePlatformInstance : ISQLitePlatformInstance
{
public ISQLitePlatform GetSQLitePlatformInstance()
{
return new SQLitePlatformAndroid();
}
}
这篇关于Xamarin.Forms在ManyToOne关系上的Sqlite-net NotSupportedException“不知道< model>"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!