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

  • Home
  • SEARCH
  • 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 8334213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:16:21+00:00 2026-06-09T03:16:21+00:00

I have a sql table with a PK column ItemID and unique index on

  • 0

I have a sql table with a PK column ItemID and unique index on column Value.

CREATE TABLE [dbo].[Item](
    [ItemID] [int] IDENTITY(1,1) NOT NULL,
    [Value] [int] NULL
)

CREATE UNIQUE NONCLUSTERED INDEX [IDX_Item_Value_U_NC] ON [dbo].[Item] 
(
    [Value] ASC
)

I’m inserting multiple records (batch insert/update) in the table using linq-sql. I do an “exists” check before inserting the record.

void MultipleInserts(List<int> values)
{
    using (var db = new DBDataContext())
    {
        foreach (var value in values)
        {
            var dbItem = db.Items
                    .Where(x => x.Value == value)
                    .SingleOrDefault();

            if (dbItem == null)
            {
                var Item = new Item()
                {
                    Value = value
                };
                db.Items.InsertOnSubmit(Item);
            }
        }
        db.SubmitChanges();
    }
}

I get the below exception due to the unique index if the list has the same values. Item table has no records.

var values = new List<int>();
values.Add(1);
values.Add(1);
MultipleInserts(values);

Exception message:

Cannot insert duplicate key row in object ‘dbo.Item’ with unique index ‘IDX_Item_Value_U_NC’.
The statement has been terminated.

How do I avoid this?

EDIT: I cannot do single inserts so I cannot move the db.SubmitChanges() call inside the for loop.

EDIT2: The answers posted so far deal with fixing the data rather than a way within linq-sql. I have put a simple example with a list of ints. But in reality I have a list of object graph to inserts and every item hits multiple tables for insert/update. I found a way using linq DataContext‘s ChangeSet (DataContext.GetChangeSet().Deletes, DataContext.GetChangeSet().Inserts, etc.) which I have posted as answer.

  • 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-06-09T03:16:23+00:00Added an answer on June 9, 2026 at 3:16 am

    The problem here is that you are inserting them both at the same time. Neither of them exist until you actually call db.SubmitChanges()

    Try moving the db.SubmitChanges() inside the loop and that should solve the problem.

    ETA: Another possibility would be changing your loop from:

    foreach (var value in values)
    

    to

    foreach (var value in values.Distinct())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a value in my MS SQL table column called XDATA like this:
Does each column of a table in SQL Server have a unique id? I've
If i have a sql table with column like this id version subversion 1
In Django I have a SQL table with a column with a list of
I have a column of JSON strings in my SQL table. I want to
I have some data in an SQL table with an XML datatype column where
I have an SQL Server 2008 database with a table that has a column
I have a SQL Server 2000 with a table containing an image column. How
I have a Money column in my SQL Server 2008 table. In my below
I have SQL table that has a varchar(8) column that occasionally has binary data

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.