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

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

国家名字
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);
            }
        }
    }
}

我是在做乱序的事情,还是完全忘记了什么?非常感谢您的任何建议。

最佳答案

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

希望这可以帮助!

关于sqlite - 使用Sqlite-net扩展的多对多关系,获得NotSupportedException:不了解System.Collections.Generic.List,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42886964/

相关文章:

SQL - 选择值等于多个值的项目

C++ SQLite 在 C 接口(interface)中导入整个 CSV 文件

c# - 用于绑定(bind)的 Xamarin.Forms MarkupExtension

xamarin - NavigationPage.SetTitleIcon - 适用于 iOS,但不适用于 Android

c# - 检索包含所有子元素(和子子元素)的对象

json - REST API 的数据库列命名

java - 图像不会保留在我的数据库中

xamarin - 显示操作表中的按钮事件

Android - 'SQLite.SQLiteConnection' 的类型初始值设定项引发异常。 ---> 系统.DllNotFoundException : e_sqlite3

sqlite - MvvmCross异步SQLite