【问题标题】:Sqlite extension is not working as expectedSqlite 扩展没有按预期工作
【发布时间】:2017-07-21 18:32:00
【问题描述】:

我正在开发一个 WinRT 应用程序。我想用sqlite-net-extensions来支持OneToMany,ManyToMany。

using SQLiteNetExtensions.Attributes;
using SQLite;

[Table("WorkFlow")]
public class Workflow
{
    [PrimaryKey, AutoIncrement]
    public int WorkflowId { get; set; }
    public string Name { get; set; }
    public int Revision { get; set; }
    [OneToMany]
    public List<Step> Steps { get; set; }
}

[Table("Step")]
public class Step
{
    public string Type { get; set; }
    public string Description { get; set; }
    [ManyToOne]
    public Workflow Workflow { get; set; }
}

当我尝试为数据库生成表时,它会引发异常:

“System.NotSupportedException”类型的异常发生在 app_name.exe 但未在用户代码中处理其他信息: 不知道 System.Collections.Generic.List`1 [app_name.Model.modelName]

这是来自SQLite.cs 中的SqlType。

但从sqlite-net-extensions homepage 的示例来看,List 属性应该可以正常工作。

这是他们示例的副本:

public class Stock
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    [MaxLength(8)]
    public string Symbol { get; set; }

    [OneToMany]      // One to many relationship with Valuation
    public List<Valuation> Valuations { get; set; }
}

public class Valuation
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }

    [ForeignKey(typeof(Stock))]     // Specify the foreign key
    public int StockId { get; set; }
    public DateTime Time { get; set; }
    public decimal Price { get; set; }

    [ManyToOne]      // Many to one relationship with Stock
    public Stock Stock { get; set; }
}

谁能给我一些解决这个问题的建议?谢谢。

【问题讨论】:

  • 我遇到了完全相同的问题,SQLite.cs 类型解析方法:string SqlType(TableMapping.Column p, bool storeDateTimeAsTicks) 没有对 List 类型的解释,因此它会抛出 NotSupportedException。我尝试使用 nuget 并添加 SQLiteNetExtensions 的源(及其参考)但没有运气......有人吗?
  • 您找到解决方案了吗?我遇到了同样的问题
  • 没关系,@redent84 给出了答案。

标签: c# windows-runtime windows-store-apps sqlite-net sqlite-net-extensions


【解决方案1】:

这通常是一个安装问题,因为 SQLite-Net Extensions 是使用 SQLite-Net 库编译的,但您正在使用另一个库运行您的应用程序。您可以尝试将PCL NuGet 包添加到您的项目中,也可以下载the sources from Git 并直接引用该项目。

【讨论】:

  • 确定我是否安装了正确的软件包的好方法是什么?我和 OP 有同样的问题,仍然没有解决,这些是我安装的 SQLite 包:SQLite.Net.Core-PCL、SQLite.Net-PCL、SQLiteNetExtensions、sqlite-net-pcl、SQLitePCLRaw.bundle_green、SQLitePCLRaw.core、SQLitePCLRaw .lib.e_sqlite3.android、SQLitePCLRaw.provider.e_sqlite3.android、SQLitePCLRaw.provider.sqlite3.ios_unified。
  • @ChristoferOhlsson 'sqlite-net-pcl' 和 'SQLite.Net.Core-PCL' 是冲突的包。确保使用 SQLite-Net Extensions 的最新 alpha 版本并删除任何其他 sqlite.net pcl 参考
  • 谢谢!我现在会努力让它发挥作用。我原以为可能会发生类似的事情,但是当我查看 SQLite-Net-PCL 时,我注意到它将 SQLite.Net.Core-PCL 列为依赖项。听起来他们不应该发生冲突?
  • 好的,所以我删除了上面提到的所有包,然后重新安装了 SQLiteNetExtensions(它添加了前面提到的两个包)。但是现在我的 DbConnection 构造函数不接受一个参数,所以我想我也需要一些其他包。除了名为“sqlite-net-pcl”的那个之外,我应该安装所有这些吗?
【解决方案2】:

尝试给 Table (Step) 一个 Primary Key 和一个 Foreign Key 引用 Table (工作流程)

[Table("Step")]
public class Step
{
    [PrimaryKey, AutoIncrement]
    public int StepId { get; set; }
    [ForeignKey(typeof(WorkFlow))]
    public int WorkFlowId { get; set; }
    public string Type { get; set; }
    public string Description { get; set; }
    [ManyToOne]
    public Workflow Workflow { get; set; }
}

【讨论】:

    【解决方案3】:

    修复:从所有项目中卸载对任何 SQLite NuGet 包的所有引用,在我的情况下,这是一个 Xamarin.Forms 解决方案。清理解决方案作为额外的预防措施(您甚至可以检查软件包是否已从软件包文件夹中卸载,以防出现文件访问问题阻止删除)。通过搜索 SQLiteNetExtensions(不是 sqlite-net-pcl)并检查是否在 Visual Studio 中选择了“包含预发布”选项(在 2017 年测试)再次安装。这将安装 sqlite-net-pcl v1.2.0 作为最小依赖项和几个文件,包括单词“Raw”。您可以在此时进行测试以检查“不知道 System.Collections.Generic.List`1”错误。对我来说,这消失了。现在应该可以安全地将 sqlite-net-pcl 更新到最新版本,在撰写本文时它是 v1.3.3。

    背景: 我在以下组合中遇到了同样的问题:

    1. sqlite-net-pcl v1.3.3
    2. SQLiteNetExtensions v2.0.0-alpha2

    在我的第一次尝试中,我首先通过 NuGet 安装了 sqlite-net-pcl,然后安装了最新的稳定版本的 SQLiteNetExtensions。在发现扩展与 sqlite-net-pcl 不兼容后,我更新到最新的预发布版本。我遇到了同样的“不知道 System.Collections.Generic.List`1”错误,发现它非常令人困惑,导致我先尝试其他事情并质疑我是否犯了错误。清理项目、删除包(从文件夹中)和恢复对我不起作用。我没有检查包配置,但使用 NuGet 管理器并与测试项目进行比较,一切都是一样的。卸载我的修复中提到的所有内容并让 SQLiteNetExtensions 为我进行依赖项检查修复了问题。我认为 NuGet 文件最初存在错误版本的 SQLiteNetExtensions 问题。

    附:当天早些时候,NuGet 为一个文件大小为 0KB 的新 Xamarin.Forms 项目下载了两个包,该项目以典型方式安装,我不得不从另一个项目中复制它们。我通常不会遇到与 NuGet 相关的问题。

    【讨论】:

      猜你喜欢
      • 2021-08-31
      • 2010-10-08
      • 2013-04-20
      • 1970-01-01
      • 2023-03-04
      • 2018-07-07
      • 1970-01-01
      • 1970-01-01
      • 2021-10-19
      相关资源
      最近更新 更多