我以为这在其他地方有介绍,但现在我看不到了。无论如何,在使用简单的 v3 查询时遇到问题。使用 SQLite ADO.NET 提供程序 1.0.65.0。我的表结构如下所示:

CREATE TABLE "SamplerData" ("RowId" INT PRIMARY KEY  NOT NULL ,"SampName" VARCHAR(128),"SampPurpose" VARCHAR(2048),"ActiveState" INTEGER NOT NULL  DEFAULT 1 )

我的 Structs1.cs 文件中有这个:
        Columns.Add(new DatabaseColumn("RowId", this)
        {
                IsPrimaryKey = true,
                DataType = DbType.Int32,
                IsNullable = false,
                AutoIncrement = false,
                IsForeignKey = false
        });

        Columns.Add(new DatabaseColumn("SampName", this)
        {
                IsPrimaryKey = false,
                DataType = DbType.AnsiString,
                IsNullable = true,
                AutoIncrement = false,
                IsForeignKey = false
        });

        Columns.Add(new DatabaseColumn("SampPurpose", this)
        {
                IsPrimaryKey = false,
                DataType = DbType.AnsiString,
                IsNullable = true,
                AutoIncrement = false,
                IsForeignKey = false
        });

        Columns.Add(new DatabaseColumn("ActiveState", this)
        {
                IsPrimaryKey = false,
                DataType = DbType.Int32,
                IsNullable = false,
                AutoIncrement = false,
                IsForeignKey = false
        });

我在 WPF 代码隐藏中有一个查询,如下所示:
SqlQuery sqlsql = new Select()
  .From( "SamplerData" )
  .Where( "ActiveState" )
  .IsEqualTo( 1 );
List<SamplerDatum> sampAll = sqlsql .ExecuteTypedList<SamplerDatum>();

设置为显示 sqlsql 值的断点显示:
{SELECT * FROM `SamplerData` WHERE ActiveState = @0}

然后代码抛出:

{“'System.Int64' 类型的对象无法转换为'System.Int32' 类型。”}

Visual Studio 中的“查找”没有显示 Int64 转换发生的位置。我知道 SQLite 使用 Int64 作为标识列,但不知道为什么/如何在 Structs 将其设为 Int32 时 SubSonic 处理转换。

帮助?!

谢谢..

最佳答案

我对 SubSonic 了解不多,但是 sqlite 的 ADO.NET 客户端对所有整数列都使用 int64。在不了解 SubSonic 的情况下,这只是一个猜测,但您可能希望将 DbType.Int32 列更改为 DbType.Int64。

最有可能的是,查询实际上执行得很好,但返回值(最初是无类型的,以 ADO.NET 方式)被拆箱到一些 SubSonic 数据结构中——它认为结构应该是 32 位整数,基于你的 DbType.Int32 语句.您不能将 long 拆箱为 int(即 (int)(object)(long)0 将抛出异常)-因此您需要告诉 SubSonic 期望 64 位整数,因为这是 SQLite 将给您的。

关于SQLite Int64 vs Int32 问题和 SubSonic ActiveRecord,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1334672/

10-10 20:31