1
votes

I am using sqlite net extensions library in my xamarin.forms application. I code in my PCL the database code and models.

When I call SQLiteConnection.CreateTable() I get an error System.NotSupportedException: Don't know about Cigars.Models.Cigar

Smoke is a child of Cigar, it has a ManyToOne relationship. Here are the models:

Smoke

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; }

}

Cigar

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
}
1
You are only passing one parameter to the SQLiteConnection constructor thus I would have to assume you are using not using Sqlite.Net.SQLiteConnection which requires a ISQLitePlatform object as the first parameter and thus SQLiteNetExtensions.Attributes.ManyToOneAttribute would not be known to that type of SQLiteConnection. - SushiHangover
@SushiHangover I was not aware of two types of the same name SQLiteConnection. This code is in my PCL, so will I have to implement something to obtain an instance of each platforms ISQLitePlatform? Also do you know what the classes that implement ISQLitePlatform are called? - James Wierzba
There are three flavors of the SQLite extensions provide, not sure which one you are using, I would follow the links depending upon which flavor you are using and look at the integration tests... bitbucket.org/twincoders/sqlite-net-extensions/src/… - SushiHangover

1 Answers

2
votes

The problem was that I had installed two different libraries for sqlite, both containing a SQLiteConnection.

I needed to use the Sqlite.Net.SQLiteConnection class for sqlite net extensions. Not the Sqlite.SQLiteConnection I was using.

As SushiHangover pointed out, this Sqlite.Net.SQLiteConnection takes a first argument of type 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();
}

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();
    }

}