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?