sqlite之使用Sqlite-net扩展的多对多关系,获得NotSupportedException:不了解System.Collections.Generic.List
lovecherry
阅读:148
2025-06-02 22:19:02
评论:0
我有一个新的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,再次清理并重建。为我工作,我想这些软件包的较新版本之间会有一些不兼容。
希望这可以帮助!
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。



