【问题标题】:How to use Oracle sequence in Servicestack Ormlite如何在 Servicestack Ormlite 中使用 Oracle 序列
【发布时间】:2014-03-27 22:25:30
【问题描述】:

我在 Oracle 中有一个表和一个序列:

CREATE TABLE USER1.TABLE1 ( 
    ID                   number(9,0)  NOT NULL,
    FIELD_1              nvarchar2(64)  NOT NULL,
    FIELD_2              nvarchar2(256),
    CONSTRAINT PK_TABLE1 PRIMARY KEY ( ID )
) ;

CREATE SEQUENCE USER1.SEQ_TABLE1 START WITH 1 ;

我还有一个用 C# 定义的类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ServiceStack.DataAnnotations;

namespace OraTest
{
    public class Table1
    {
        [AutoIncrement]
        public int      Id { get; set; }
        public string   Field_1 { get; set; }
        public string   Field_2 { get; set; }
    }
}

我还有一个 DTO 类和一个服务类和数据存储库类,其中我有一个数据插入代码:

...
using (var db = DbConnectionFactory.OpenDbConnection())
{
  db.Insert(data);
}
...

当我尝试插入记录时,我得到“ORA-02289:序列不存在”。

我如何告诉 OrmLite 使用哪个序列?

【问题讨论】:

    标签: oracle ormlite-servicestack


    【解决方案1】:

    您得到 ORA-02289 的原因是因为没有信息可以在哪里查找序列。

    您需要做的就是在定义数据模型类的类的顶部添加 [Sequence()] 属性。如果您将在 Oracle 数据库中定义一个数据表 TABLE1 和一个相应的序列 SEQ_TABLE1,那么您的示例将是:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using ServiceStack.DataAnnotations;
    
    namespace OraTest
    {
        public class Table1
        {
            [AutoIncrement]
            [Sequence("SEQ_TABLE1")]
            public int      Id { get; set; }
            public string   Field_1 { get; set; }
            public string   Field_2 { get; set; }
        }
    }
    

    现在应该使用定义的顺序将新记录添加到 Oracle 表中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-05
      相关资源
      最近更新 更多