locked
Cannot insert explicit value for identity column in table 'Skill_Comment_D' when IDENTITY_INSERT is set to OFF. ??? RRS feed

  • Question

  • Hi,

    Ok, I'm stumped - i'm trying insert a record using EF.
    now the primary key "Id" is an identity int,
    and when i instantiate a new instance of the entity i do NOT provide a value for the Id field,
    however it is still complaining that i should NOT explicitily provide a value???

    but i'm not??

    this is the code to instantiate a new instance of the entity which i'm trying to add via EF:

       SkillComment skillComment = new SkillComment()
       {
        Comment=kryptonRichTextBox1.Text,
        EmployeeNo="why?",
        UpdatedBy=System.Environment.UserName,
        UpdatedDate = DateTime.Now,
        SkillRating = ((UserSkill)bsUserSkill.Current).SkillRating,
        ProposedSkillRating = ((UserSkill)bsUserSkill.Current).ProposedSkillRating ,
        UserSkill = (UserSkill) bsUserSkill.Current
       };


    this is the exception:


    {An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is:
    System.Data.UpdateException: An error occurred while updating the entries. See the InnerException for details. ----> System.Data.SqlClient.SqlException: Cannot insert explicit value for identity column in table 'Skill_Comment_D' when IDENTITY_INSERT is set to OFF.
       at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
       at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
       at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
       at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
       at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
       at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
       at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
       at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
       at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
       at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues)
       at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
       --- End of inner ExceptionDetail stack trace ---
       at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
       at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
       at System.Data.Objects.ObjectContext.SaveChanges(Boolean acceptChangesDuringSave)
       at System.Data.Objects.ObjectContext.SaveChanges()
       at EntityFrameworkTools.ModelAccess`1.AddEntity[EntityType](EntityType entity)
       at SkillMatrixService.SkillMatrixService.AddSkillComment(SkillComment skillComment) in C:\Development\Tokyo\GroupIT\DEV\SkillMatrixService\SkillMatrixService\SkillMatrixService.cs:line 550}
    I.W Coetzer
    Saturday, February 20, 2010 3:07 PM

Answers

  • Got it working,

    changed the database and put identity int for the Id field on,
    BUT
    I never updated the EF MODEL!!!

    After updating the EF model it is now working.

    So.... no matter WHAT changes one makes to the database schema, one should UPDATE / REFRESH the EF model!
    I.W Coetzer
    • Marked as answer by I.W Coetzer Monday, February 22, 2010 7:57 AM
    Saturday, February 20, 2010 3:59 PM