【问题标题】:SQLite Int64 vs Int32 Problem and SubSonic ActiveRecordSQLite Int64 vs Int32 问题和 SubSonic ActiveRecord
【发布时间】:2009-08-26 13:24:35
【问题描述】:

我以为这在其他地方有报道,但我现在看不到。无论如何,一个简单的 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 为何/如何处理转换。

帮忙?!

谢谢..

【问题讨论】:

    标签: sqlite activerecord subsonic subsonic3


    【解决方案1】:

    我们在最新版本中修复了这个问题 - SQLite.tt 文件没有正确地将整数 PK 转换为 long (int 64)。如果您在 Github 上获取最新信息,则应该可以解决。确保从模板项目中获取。

    【讨论】:

      【解决方案2】:

      我对 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 将给您的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-15
        相关资源
        最近更新 更多