Mysql – Insert using linq templates not returning the id – MySQL

insertlinqMySQLsubsonic3

I'm using the latest subsonic dll and the latest linq templates from github. The db i'm inserting into is MySQL. Id column on table is primary key auto increment.

Versions:
Subsonic.Core.dll – 3.0.0.3 – (November 18, 2009 Merged pulls from Github).

LinqTemplates – July 29, 2009.

MySQL.Data.CF.dll – 6.1.2.0.

The row is inserted but the id is returned as 0.

Example of the insert:

mysqldb db = new mysqldb.mysqldbDB();
int ID = db.Insert.Into<db.myTable>(
    r => r.message,
    r => r.name,
    r => r.status).Values(
    message,
    name,
    status).Execute();

Am I doing something wrong? Shouldn't the new id be returned, not zero?

Best Answer

Found the bug in subsonic core.

It's in Subsonic.Core.Query.Insert.cs

The Execute method does not have a condition for id's returned that are of type long.

I've rewritten the method in my local version to:

    public int Execute()
    {
        int returner = 0;

        object result = _provider.ExecuteScalar(GetCommand());
        if(result != null)
        {
            if(result.GetType() == typeof(decimal))
                returner = Convert.ToInt32(result);
            else if (result.GetType() == typeof(int))
                returner = Convert.ToInt32(result);
            else if (result.GetType() == typeof(long))
                returner = Convert.ToInt32(result);
            else
                returner = Convert.ToInt32(result);
        }
        return returner;
    }

I've changed the multiple if statements to else if's and added the type comparison of long. Also I've added the final else condition which does a convert to int. Not sure if that's such a good idea but it works for me.

If someone wants to update the source great. If i find time sometime soon i'll update it myself.