0

I'm using a method that creates new Entries in a table. The method looks like this:

public void CreateProcessingTime(Guid userId, int activityId, int min, int max, int avg)
        {
            var t = new T_USER_ACTIVITY_PROCESSING_TIME
            {
                UAP_USR_ID = userId,
                UAP_ACT_ID = activityId,
                UAP_P_TIME_MIN = min,
                UAP_P_TIME_MAX = max,
                UAP_P_TIME_AVG = avg
            };
            _dbEntities.T_USER_ACTIVITY_PROCESSING_TIME.Add(t);
            _dbEntities.SaveChanges();
        }

Upon the SaveChanges(); it throws an error:

SqlException: Cannot insert explicit value for identity column in table when IDENTITY_INSERT is set to OFF.

The SQL I used to generate the table was:

-- Creating table 'T_USER_ACTIVITY_PROCESSING_TIME'
CREATE TABLE [dbo].[T_USER_ACTIVITY_PROCESSING_TIME] (
    [UAP_ID] int IDENTITY (1,1) NOT NULL,
    [UAP_USR_ID] uniqueidentifier  NOT NULL,
    [UAP_ACT_ID] int  NOT NULL,
    [UAP_P_TIME_MIN] int  NOT NULL,
    [UAP_P_TIME_AVG] int  NOT NULL,
    [UAP_P_TIME_MAX] int  NOT NULL
);
GO
SET IDENTITY_INSERT [T_USER_ACTIVITY_PROCESSING_TIME] ON

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------    
-- Creating primary key on [UAP_ID] in table 'T_USER_ACTIVITY_PROCESSING_TIME'
ALTER TABLE [dbo].[T_USER_ACTIVITY_PROCESSING_TIME]
ADD CONSTRAINT [PK_T_USER_ACTIVITY_PROCESSING_TIME]
    PRIMARY KEY CLUSTERED ([UAP_ID] ASC);
GO

I tried setting SET IDENTITY_INSERT T_USER_ACTIVITY_PROCESSING_TIME ON on the SQL Server Manager, but it did not resolve the issue. My next guess would be that I had to change something on the .edmx but I can't seem to find the correct place. enter image description here

What else can I do to resolve the error?

Peter
  • 1,802
  • 2
  • 20
  • 44
  • 2
    Seems like you edmx file is not updated with database columns setting. Check UAP_ID is map as identify column or not? – Always_a_learner May 22 '20 at 17:48
  • Does this answer your question? [Cannot insert explicit value for identity column in table 'table' when IDENTITY\_INSERT is set to OFF](https://stackoverflow.com/questions/1334012/cannot-insert-explicit-value-for-identity-column-in-table-table-when-identity) – hujtomi May 24 '20 at 10:37
  • @Always_a_learner, Indeed, that was the problem. Thank you – Peter May 25 '20 at 16:24
  • Great keep learning – Always_a_learner May 25 '20 at 16:30

0 Answers0