【问题标题】:Insert using linq templates not returning the id - MySQL使用不返回 id 的 linq 模板插入 - MySQL
【发布时间】:2009-11-26 16:52:43
【问题描述】:

我正在使用来自 github 的最新 subsonic dll 和最新的 linq 模板。我要插入的数据库是 MySQL。表上的id列是主键自增。

版本: Subsonic.Core.dll - 3.0.0.3 -(2009 年 11 月 18 日从 Github 合并拉取)。

LinqTemplates - 2009 年 7 月 29 日。

MySQL.Data.CF.dll - 6.1.2.0.

该行被插入,但 id 返回为 0。

插入示例:

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

我做错了吗?是不是应该返回新的id,而不是零?

【问题讨论】:

    标签: mysql linq insert subsonic3


    【解决方案1】:

    发现亚音速核心中的错误。

    它在 Subsonic.Core.Query.Insert.cs 中

    Execute 方法没有返回 long 类型的 id 的条件。

    我已将本地版本中的方法重写为:

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

    我已将多个 if 语句更改为 else if,并添加了 long 的类型比较。我还添加了最终的 else 条件,它可以转换为 int。不确定这是否是个好主意,但它对我有用。

    如果有人想更新源代码。如果我有时间很快我会自己更新。

    【讨论】:

    • 别忘了报告错误 ;)
    猜你喜欢
    • 2011-05-19
    • 1970-01-01
    • 1970-01-01
    • 2012-01-15
    • 2011-11-06
    • 2014-09-06
    • 2012-09-12
    • 2017-09-13
    • 2019-12-29
    相关资源
    最近更新 更多