SQLite Int64 与 Int32 问题和 SubSonic ActiveRecord

发布于 2024-08-02 11:53:15 字数 2017 浏览 8 评论 0原文

我以为这在其他地方有所涉及,但现在没有看到。不管怎样,一个简单的 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 处理转换。

帮助?!

谢谢..

I thought this was covered elsewhere but I don't see it now. Anyway, having a problem with a simple v3 query. Using SQLite ADO.NET provider 1.0.65.0. My table structure looks like this:

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

My Structs1.cs file has this in it:

        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
        });

I have a query in a WPF codebehind that looks like this:

SqlQuery sqlsql = new Select()
  .From( "SamplerData" )
  .Where( "ActiveState" )
  .IsEqualTo( 1 );
List<SamplerDatum> sampAll = sqlsql .ExecuteTypedList<SamplerDatum>();

A breakpoint set to show the value of sqlsql shows this:

{SELECT * FROM `SamplerData` WHERE ActiveState = @0}

Then the code throws with:

{"Object of type 'System.Int64' cannot be converted to type 'System.Int32'."}

A "find" in Visual Studio didn't show me where the Int64 conversion was happening. I understand that SQLite uses Int64 for identity columns, but not why/how SubSonic is handling the conversion when the Structs is making it Int32 anyway.

Help?!

Thanks..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

非要怀念 2024-08-09 11:53:15

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

We fixed this with the latest release - the SQLite.tt file didn't correctly translate the integer PK to long (int 64). If you grab the latest bits at Github it should be solved. Make sure you grab from the templates project.

瞄了个咪的 2024-08-09 11:53:15

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

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

I don't know much about SubSonic, but the ADO.NET client for sqlite uses int64 for all integer columns. Without knowing SubSonic, this is just a guess, but you may want to change the DbType.Int32 columns to DbType.Int64.

Most likely, the query is actually executing fine, but the return values (untyped initially, in ADO.NET fashion) are being unboxed into some SubSonic data structures - structures it thinks should be 32-bit integers, based on your DbType.Int32 statement. You cant unbox a long into an int (i.e. (int)(object)(long)0 will throw an Exception) - so you need to tell SubSonic to expect 64-bit integers, since that's what SQLite is going to give you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文