gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 23:07:11 35 4
gpt4 key购买 nike

我有一个新的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/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com