I have a new Xamarin Forms application where I'm trying to make use of Sqlite-net and Sqlite-net extensions for an ORM. I've followed This guide to create the n:n. Here are my two objects, as well as their intermediate object:
Nation
using Sqlite;
using SQLiteNetExtensions.Attributes;
using System.Collections.Generic;
namespace MyNamespace.Models
{
public class Nation
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Name { get; set; }
public string Adjective { get; set; }
public double Frequency { get; set; }
[ManyToMany(typeof(NationFirstName))]
public List<FirstName> FirstNames { get; set; }
}
}
FirstName
using SQLite;
using SQLiteNetExtensions.Attributes;
using System.Collections.Generic;
namespace MyNamespace.Models
{
public class FirstName
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Name { get; set; }
[ManyToMany(typeof(NationFirstName))]
public List<Nation> Nations { get; set; }
}
}
NationFirstName
using SQLite;
using SQLiteNetExtensions.Attributes;
namespace OffseasonGM.Models
{
public class NationFirstName
{
[ForeignKey(typeof(Nation))]
public int NationId { get; set; }
[ForeignKey(typeof(FirstName))]
public int FirstNameId { get; set; }
}
}
And so, in my app's starting point, App.xaml.cs, I try to first create the NationFirstNameRepository, which works well. And next, I try to create the NationRepository, where I get an exception in the constructor. On the CreateTable() line:
[ERROR] FATAL UNHANDLED EXCEPTION: System.NotSupportedException: Don't know about System.Collections.Generic.List`1[OffseasonGM.Models.FirstName]
NationRepository.cs
using MyNamespace.Models;
using SQLite;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
namespace MyNamespace.Assets.Repositories
{
public class NationReposistory
{
SQLiteConnection connection;
public NationReposistory(string dbPath)
{
connection = new SQLiteConnection(dbPath);
var entriesCreatedCount = connection.CreateTable<Nation>();
if (entriesCreatedCount < 1)
{
SeedNations();
}
}
public void AddNewNation(string name, string adjective, double frequency)
{
try
{
connection.Insert(new Nation { Name = name, Adjective = adjective, Frequency = frequency });
}
catch (Exception e)
{
//TODO: Error handling.
}
}
public List<Nation> GetAllNations()
{
return connection.Table<Nation>().ToList();
}
private void SeedNations()
{
var assembly = typeof(MainPage).GetTypeInfo().Assembly;
var stream = assembly.GetManifestResourceStream("MyNamespace.Nations.txt");
using (var reader = new StreamReader(stream))
{
var line = reader.ReadLine().Split('|');
var name = line[0];
var adjective = line[1];
var frequency = double.Parse(line[2]);
AddNewNation(name, adjective, frequency);
}
}
}
}
Am I doing something out of order, or altogether forgetting something? Very thankful for any advice.