OrmLite-Oracle:错误的插入语句

时间:2019-05-31 09:30:36

标签: servicestack ormlite-servicestack

我正在测试ServiceStack.OrmLite.Oracle(5.5.1),但是在创建具有Sequence属性的模型时无法将数据保存到数据库中。尝试打印插入语句,我发现sql不正确。该如何解决?

using System;
using System.Data;
using NUnit.Framework;
using ServiceStack.DataAnnotations;
using ServiceStack.OrmLite;

namespace Tests
{
    public class DatabaseTest
    {
        private readonly IDbConnection _db;

        public DatabaseTest()
        {
            var dbFactory = new OrmLiteConnectionFactory(
                @"Data Source = (DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = ora-test)(PORT = 1521))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = twcms12c))); User Id=scott; Password=Ab123456",
                OracleDialect.Provider);
            _db = dbFactory.OpenDbConnection();
        }

        [Test]
        public void EndPointTest()
        {

            var customer = new Customer {Id=1,Name = "Customer 1"};
            Console.WriteLine(_db.ToInsertStatement(customer));

            customer = new Customer {Name = "Customer 2"};
            Console.WriteLine(_db.ToInsertStatement(customer));
        }


        public class  Customer
        {
            [Sequence("Customer_Id_Seq")]
            public long Id { get; set;}
            public string Name { get; set; }
        }
    }
}

插入客户(ID,名称)值(1,“客户1”)

从双重中选择Customer_Id_Seq.NEXTVAL

1 个答案:

答案 0 :(得分:2)

在ServiceStack.OrmLite.Oracle(5.5.1)中,方法GetNextValue(ServiceStack.OrmLite.Oracle.OracleOrmLiteDialectProvider.cs)中存在一个错误:

        private object GetNextValue(IDbCommand dbCmd, string sequence, object value)
        {
            if (value.ToString() != "0")
            {
                object retObj;
                if (long.TryParse(value.ToString(), out var nv))
                {
                    LastInsertId = nv;
                    retObj = LastInsertId;
                }
                else
                {
                    LastInsertId = 0;
                    retObj = value;
                }
                return retObj;
            }

            dbCmd.CommandText = $"SELECT {Quote(sequence)}.NEXTVAL FROM dual";
            long result = (long)dbCmd.LongScalar();
            LastInsertId = result;
            return result;
        }

我将其更改为:

        private object GetNextValue(IDbCommand dbCmd, string sequence, object value)
        {
            if (value.ToString() != "0")
            {
                object retObj;
                if (long.TryParse(value.ToString(), out var nv))
                {
                    LastInsertId = nv;
                    retObj = LastInsertId;
                }
                else
                {
                    LastInsertId = 0;
                    retObj = value;
                }
                return retObj;
            }

            var lastSql = dbCmd.CommandText;
            dbCmd.CommandText = $"SELECT {Quote(sequence)}.NEXTVAL FROM dual";
            long result = (long)dbCmd.LongScalar();
            LastInsertId = result;
            dbCmd.CommandText = lastSql;
            return result;
        }

,效果很好。 P / s:我创建了一个请求请求,该请求已被ServiceStack接受。