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 290115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:58:06+00:00 2026-05-12T05:58:06+00:00

Although this probably isn’t best practice, I am trying to clear a series of

  • 0

Although this probably isn’t best practice, I am trying to clear a series of records from a database table, and then insert a series of records – some of which may have been in the original series, and others which may be new. I’m using linqtosql in C#. Pseudo-code I have to do this is below; it fails with “Cannot add an entity with a key that is already in use.”

using (dbDataContext context = new dbDataContext()
{
  // I've also tried using table.LinkedTable.Clear(); but that shows the same error
  while (table.LinkedTable.Count() > 0)
  {
    table.LinkedTable.RemoveAt(0);
  }

  foreach (ListItem item in SelectedItems.Items)
  {
    LinkedTable lt = new LinkedTable();
    lt.id = table.id;
    lt.SomeValue = item.SelectedValue;
    table.LinedTable.Add(lt);
  }
  context.SubmitChanges();
}

It seems like linqtosql is not taking note that I have removed items, before it tries to add items, causing a duplication error. Does anyone have any suggestions to correct this?

EDIT
If I add a SubmitChanges after doing the deletes, I then end up with this message.

“An attempt was made to remove a relationship between a table and a LinkedTable. However, one of the relationship’s foreign keys (LinkedTable.Id) cannot be set to null.”

  • 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-12T05:58:06+00:00Added an answer on May 12, 2026 at 5:58 am

    You could try to use the ITable.DeleteAllOnSubmit method:

    using (dbDataContext context = new dbDataContext()
    {
      // table.LinkedTable is a EntitySet of LinkedTable objects,
      // we remove from the "LinkedTable" table, the records that
      // are related to the "table" object:
      context.LinkedTable.DeleteAllOnSubmit(table.LinkedTable);
    
      foreach (ListItem item in SelectedItems.Items)
      {
        LinkedTable lt = new LinkedTable();
        lt.id = table.id;
        lt.SomeValue = item.SelectedValue;
        table.LinedTable.Add(lt);
      }
      context.SubmitChanges();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Although this code is from an algorithm text, I have a bit of a
This is a follow on from my previous question although this is about something
Context: I'm trying to improve the values returned by the iPhone CLLocationManager, although this
Although this question is about JFlex, it probably applies to other scanner generators such
This is probably easy, although I cant seem to find the solution. In my
Although this is FAQ i still couldn't find a suitable solution. I'd like to
A colleague of mine asked me to write a homework for him. Although this
According to this website : Although GSM operates in duplex (separate frequencies for transmit
I am using the jQuery-File-Upload widget (although I believe this question can generalize to
I have the SQL2008 version of SMO and although it uses this class internally

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.