-1

THIS IS NOT DUPLICITY I am NOT setting value to identity column!

I cant add value in my Table. I think all set good but this error keeps comming:

"Cannot insert explicit value for identity column in table 'MainQueue' when IDENTITY_INSERT is set to OFF."

My table:

CREATE TABLE [dbo].[MainQueue] (
    [Id]               INT      IDENTITY (1, 1) NOT NULL,
    [Created]          DATETIME NOT NULL,
    [IdOffice]         INT      NOT NULL,
    [IdCategory]       INT      NOT NULL,
    [StartProcessTime] DATETIME NULL,
    [EndProcessTime]   DATETIME NULL,
    [IdUser]           INT      NULL,
    [Sms]              BIT      CONSTRAINT [DF__MainQueue__Sms__412EB0B6] DEFAULT ((0)) NOT NULL,
    [OrderNumber]      INT      NOT NULL,
    [IdSms]            INT      NULL,
    [UserWindowNumber] INT      NULL,
    CONSTRAINT [PK__MainQueu__3214EC0783954F32] PRIMARY KEY CLUSTERED ([Id] ASC),
    CONSTRAINT [FK_MainQueue_Category] FOREIGN KEY ([IdCategory]) REFERENCES [dbo].[Category] ([Id]),
    CONSTRAINT [FK_MainQueue_Office] FOREIGN KEY ([IdOffice]) REFERENCES [dbo].[Office] ([Id]),
    CONSTRAINT [FK_MainQueue_User] FOREIGN KEY ([IdUser]) REFERENCES [dbo].[User] ([Id]),
    CONSTRAINT [FK_MainQueue_SmsQueue] FOREIGN KEY ([IdSms]) REFERENCES [dbo].[SmsQueue] ([Id])
);

My code for adding:

var queueItem = new MainQueue();
queueItem.IdOffice = officeId;
queueItem.IdCategory = categoryId;
queueItem.Created = DateTime.Now;
queueItem.OrderNumber = orderNum;

dc.MainQueues.InsertOnSubmit(queueItem);
dc.SubmitChanges();

THIS IS NOT DUPLICITY I am NOT setting value to identity column!

RadoInko
  • 33
  • 1
  • 9

1 Answers1

0

Your described ID column is an autoincrement identity column, that means you cannot pass any explicit value for the ID, after an insert the DBS will do fill the ID. Remove the ID column in your Insert method and do not pass a parameter for the Id when calling your method.

If you really need to insert an explicit value for the ID, either make the column not an IDENTITY, instead primary or turn IDENTITY INSERT OFF before you call the insert and turn in back on afterwards.

Raul
  • 2,745
  • 1
  • 23
  • 39