使用Sqlite-net扩展的多对多关系,获取NotSupportedException:不了解System.Collections.Generic.List

时间:2017-03-19 13:10:32

标签: sqlite xamarin sqlite-net sqlite-net-extensions

我有一个新的Xamarin Forms应用程序,我正在尝试为ORM使用Sqlite-net和Sqlite-net扩展。我跟着This guide创建了n:n。这是我的两个对象,以及它们的中间对象:

国家

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

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

所以,在我的应用程序的起点App.xaml.cs中,我尝试首先创建NationFirstNameRepository,它运行良好。接下来,我尝试创建NationRepository,在构造函数中我得到一个异常。在CreateTable()行:

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

我是在做一些乱序,还是完全忘了什么?非常感谢任何建议。

1 个答案:

答案 0 :(得分:0)

这里的错误相同。 我卸载包含PCL的所有nugets,删除bin和obj文件夹,清理解决方案,添加SQLiteNetExtensions 1.3.0和SQLite.Net-PCL 3.1.1 nugets,再次清理并重建。为我工作,我想这些软件包的新版本之间存在一些不兼容性。

希望这有帮助!