Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7534411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:00:28+00:00 2026-05-30T06:00:28+00:00

I am using IDENTITY columns in my SQL Server tables. I have a unit

  • 0

I am using IDENTITY columns in my SQL Server tables.

I have a unit test where I delete a data record with a certain id.

When I insert the testdata I get this exception:

Cannot insert explicit value for identity column in table ‘MyTablename’ when IDENTITY_INSERT is set to OFF

Then I do a quick test in my management studio:

SET IDENTITY_INSERT MyTablename ON

It says Command(s) completed successfully.

I rerun the unit test but still get the same error.

How do I go about that in general? I want that my unit testing database with all tables should allow me to insert a value manually for an identity column.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T06:00:29+00:00Added an answer on May 30, 2026 at 6:00 am

    SET IDENTITY_INSERT is session-specific. You can’t set it to true in SSMS and then go run a unit test in some other rig. The SET command needs to be part of the unit test code.

    Also, you can only set this setting to one table at a time, so it doesn’t seem like your unit test will work very well unless you set each table as you need it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using SQL-Server 2005. I have table with many columns, rows i select have
I'm using SQL Server and I want to use identity constraint in it I
I'm using SQL Server 2K8 and have a table used solely to generate ids
How with SQL server bulk insert can I insert into multiple tables when there
I'm using SQL Server 2008. I have a database table that looks like this
I have a SQL script that inserts data (via INSERT statements currently numbering in
In a SQL Server table I have the following 2 columns: RowId : primary
In SQL Server there is the ability to INSERT all of the data from
Using : SQL Server 2008, Entity Framework I am summing columns in a table
I am using SQL server 2005. I have a table like this - ID

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.