Building two tables with relation fails

120 views Asked by At

I am trying to build up two tables and a relationship between them in a Xamarin cross plattform application. Here's the code for the tables:

[Table("DetailTypes")]
public class DetailType
{
    [PrimaryKey, AutoIncrement]
    public int ID { get; set; }
    public string Detail { get; set; }
}

and

[Table("DateDetails")]
public class DateDetail
{
    [PrimaryKey, AutoIncrement]
    public int ID { get; set; }

    public int ID_Date { get; set; }

    public string Wert { get; set; }

    public DateTime Datum { get; set; }

    [ForeignKey(typeof(DetailType))]
    public int DetailTypeId { get; set; }

    [OneToOne]
    public DetailType DetailType { get; set; }
}

The tables are created in code as follows:

    public ItemDatabase(string dbPath)
    {
        database = new SQLiteAsyncConnection(dbPath);

        database.CreateTableAsync<DateDetail>().Wait(); //Error is thrown here
        database.CreateTableAsync<DetailType>().Wait(); 
    }

When running the code I get the error "An unhandled exception occured." in the marked line. The first table is created at that point. What am I doing wrong?

Another question: Is it important to create the tables in a certain order as they are dependent on eachother?

EDIT:

StackTrace of the exception (System.NotSupportedException):

at System.Threading.Tasks.Task.ThrowIfExceptional (System.Boolean includeTaskCanceledExceptions) [0x00011] in <896ad1d315ca4ba7b117efb8dacaedcf>:0 at System.Threading.Tasks.Task.Wait (System.Int32 millisecondsTimeout, System.Threading.CancellationToken cancellationToken) [0x00043] in <896ad1d315ca4ba7b117efb8dacaedcf>:0 at System.Threading.Tasks.Task.Wait () [0x00000] in <896ad1d315ca4ba7b117efb8dacaedcf>:0 at .CODE.Data.ItemDatabase..ctor (System.String dbPath) [0x00028] in D:\CODE_COPY\EigeneProjekte\CN__Xamarin*********\CODE\Data\ItemDatabase.cs:22

EDIT 2: If I take a closer look at the Exception I find the following message:

Don't know about XXX.CODE.Models.DetailType

This is understandable, as the table DetailType has not been built at that this point. But how am I supposed to create relations via ORM then? Is there a possibility to create all tables at once to ensure that the relations are correct and no table is missing?

2

There are 2 answers

1
redent84 On BEST ANSWER

All relationship annotations inherit from SQLite-Net Ignore attribute so the inner layer of SQLite-Net ignores this attribute it's correctly processed by sqlite-net-extensions. If SQLite-Net is not recognizing the Ignore annotation it's probably because there are conflicting versions of SQLite-Net installed as dependencies.

TL;DR: Check your dependencies and remove any duplicated SQLite-Net library

Depending on the platform, it may be easier to remove the sqlite-net-extensions nuget package and simply copy the sources to the project.

0
ASpirin On

You can make more than 1 table in a single call But as it iteates through provided types and calls function for each type separately, you have to define the sequense yourself