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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:47:19+00:00 2026-05-25T20:47:19+00:00

Is it possible to SubmitChanges and ignore deletes that cause Foreign Key constraint errors?

  • 0

Is it possible to SubmitChanges and ignore deletes that cause Foreign Key constraint errors?

Say I have object A and B. B’s Primary Key is a Foreign Key in A. Now I change a record A in which I replace B for another B and delete the original B. An error will occur when the deleted B is referenced in another record A (or other tables containing B as Foreign Key). Is it possible to ignore the error and let the changes be made in the database, without deleting the old B?

A rec_a = (from a in db.As where a.id == some_id).First();
B rec_b_old = rec_a.B;
rec_a.B = null;
db.Bs.DeleteOnSubmit(rec_b_old);
rec_a.B = some_other_b;
db.SubmitChanges();
  • 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-25T20:47:20+00:00Added an answer on May 25, 2026 at 8:47 pm

    Make two calls to SubmitChanges():

    A rec_a = (from a in db.As where a.id == some_id).First();
    B rec_b_old = rec_a.B;
    rec_a.B = null;
    rec_a.B = some_other_b;
    db.SubmitChanges();
    db.Bs.DeleteOnSubmit(rec_b_old);
    try
    {
        db.SubmitChanges();
    }
    catch(SqlException) { } // Ignore failed delete.
    

    The second one might fail and in that case just ignore it. It is possible to try to submit everything, dig out the failing update/delete, remove it from the pending list and retry. However it requires far more code so I don’t think it’s worth to do it here.

    Another solution is to put a trigger on the A table that deletes the B record if it is orphaned.

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

Sidebar

Related Questions

I have a Customer table with a Primary key (int auto increment) and an
I have a number of methods that ultimately call my this._context.SubmitChanges method. Because all
I have an ObservableCollection<Widget> that on NotifyCollectionChangedAction.Add executes context.Widget.InsertOnSubmit(addedWidget) . Before I execute context.SubmitChanges()
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
I have a multithreaded application that is using LinqToSql. One of the things I
is it possible to use a datacontext or LINQ when you have an XML
Possible Duplicate: Python urllib2 Progress Hook I have a script which uploads a file
Possible Duplicate: Split A4 PDF page into two A5 and back again I have
I insert a new object into LINQ-to-SQL DataContext without calling SubmitChanges() yet: MyDataContext db
I have a Silverlight application that interfaces with an SQL database. It does this

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.