3

I am using Ormlite to insert a record as follows:

public static Address Add(Address model)
    {
        using (IDbConnection db = DbFactory.OpenDbConnection())
            {
                db.Insert(model);
                var lastId = db.GetLastInsertId();
                model.Id = (int) lastId;
                return model;
            }
    }

However on the line var lastId = db.GetLastInsertId();

I get the following error:

Message: Specified cast is not valid.

Source: ServiceStack.OrmLite

Stacktrace: at ServiceStack.OrmLite.OrmLiteReadExtensions.GetLongScalar(IDbCommand dbCmd) in c:\src\ServiceStack.OrmLite\src\ServiceStack.OrmLite\OrmLiteReadExtensions.cs:line 793
at ServiceStack.OrmLite.ReadConnectionExtensions.Exec[T](IDbConnection dbConn, Func`2 filter) in c:\src\ServiceStack.OrmLite\src\ServiceStack.OrmLite\Expressions\ReadConnectionExtensions.cs:line 31

this points to the following ormlite code:

public static long GetLongScalar(this IDbCommand dbCmd)
    {
        var result = dbCmd.ExecuteScalar();
        if (result is DBNull) return default(long);
        if (result is int) return (int)result;
        if (result is decimal) return Convert.ToInt64((decimal)result);
#### this line here is 793 if (result is ulong) return Convert.ToInt64(result);
        return (long)result;
    }   

The record actually goes into the database just fine, and my first column is int(11) in a mysql database, and is the first column with a name of Id.

Schema:

CREATE TABLE IF NOT EXISTS `Address` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
  `Street` varchar(255) NOT NULL,
  `StreetTwo` varchar(255) DEFAULT NULL COMMENT 'Street 2:',
  `Town` varchar(100) DEFAULT NULL,
  `City` varchar(100) NOT NULL,
  `County` varchar(255) DEFAULT NULL,
  `Country` varchar(255) DEFAULT NULL,
  `PostCode` varchar(15) DEFAULT NULL COMMENT 'Post Code:',
  `Notes` text,
  `Enabled` tinyint(1) NOT NULL DEFAULT '1',
   PRIMARY KEY (`Id`)
 ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=13 ;

I use this ormlite with mysql ALL the time, and i have never, ever had this issue, and i am left scratching my head for the first time, in a long time.

I am using the nuget package Ormlite.MySQL 3.9.59 and is the most up to date package available on nuget.

This would not be so strange, if it was not for the records actually going in. This seems to be some issue with executeScaler and the returned value ????

Any help much appreciated here.

4

1 回答 1

4

我觉得这个版本有点问题ServiceStack.Ormlite。我能够重现这个问题。只需使用以前的版本即可。

您可以使用Nuget Packet Manager Console. 如果您不知道如何使用 Packet Manager 控制台安装旧版本,那么这里是:

Install-Package Package-Name-Here -Version Version-You-Want-To-Install
于 2013-08-31T03:00:32.317 回答