【问题标题】:How do you configure ormlit to support an autoincrement column that is not the primary key?如何配置 ormlit 以支持不是主键的自动增量列?
【发布时间】:2013-08-06 19:28:25
【问题描述】:

我有一张桌子

CREATE TABLE [dbo].[ServiceTestCase](
    [SSN] [int] IDENTITY(600000001,1) NOT NULL,
    [Description] [varchar](max) NULL,
    [EmbeddedResponse] [varchar](max) NULL,
    [ResponseType] [varchar](50) NULL,
    [DocumentType] [varchar](50) NULL,
    [Id] [uniqueidentifier] NOT NULL,
    [ServiceType] [varchar](50) NOT NULL,
 CONSTRAINT [PK_TestCase] PRIMARY KEY CLUSTERED 

我的班级

public class ServiceTestCase
    {
        public ServiceTestCase ()
        {

        }
        public string ServiceType { get; set; }
          [ServiceStack.DataAnnotations.AutoIncrement]
        public Guid Id { get; set; }
        [ServiceStack.DataAnnotations.AutoIncrement]
        public long SSN { get; set; }
        public string Description { get; set; }
        public string EmbeddedResponse { get; set; }
        public EmbeddedResponseType ResponseType { get; set; }
        public EmbeddedDocumentType DocumentType { get; set; }
    }

当我调用 db.Insert (new ServiceTestCase {/* Id = testId, */ServiceType = "Credit" }); 我收到以下错误: System.Data.SqlClient.SqlException:当 IDENTITY_INSERT 设置为 OFF 时,无法在表“ServiceTestCase”中插入标识列的显式值。

如何让 ormlite 忽略自动增量字段,以便插入项目?

【问题讨论】:

    标签: ormlite-servicestack


    【解决方案1】:

    您可以使用 [ServiceStack.DataAnnotations.AutoIncrement] 注释 Id。

    来源:https://groups.google.com/forum/#!msg/servicestack/JM09UGMZpkY/Klnmwq5pWoMJ

    【讨论】:

    • 问题中的代码已经有了这个注解。我不知道为什么我最初错过了它,为什么有两个人觉得这很有帮助......
    【解决方案2】:

    对于遇到此问题的其他人,[ServiceStack.DataAnnotations.Compute] 属性告诉 OrmLite 忽略插入/更新的列。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-24
      • 2013-11-05
      • 2014-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-13
      • 1970-01-01
      相关资源
      最近更新 更多