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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:34:39+00:00 2026-05-18T02:34:39+00:00

First, a little background (greatly simplified): I have two classes, one called Entity and

  • 0

First, a little background (greatly simplified): I have two classes, one called Entity and one called Item which a subclass of Entity. They each store their individual properties in seperate SQL 2008 tables using TableAdapters generated by a strongly typed DataSet. The Entity class has a Save() method that looks like this:

Public Sub Save()
  entityTableAdapter.Update(entityRow)
End Sub

and the Item class has a Save method which looks like this:

Public Overrides Sub Save()
  MyBase.Save
  itemTableAdapter.Update(itemRow)
End Sub

This works well except that if inserting an Item row fails then I am left with an orphaned Entity row in the database so I extended the TableAdapters and Save methods to use a transaction like so:

Entity Save method:

Public Sub Save(tx as SqlTransaction)
  entityTableAdapter.Connection = tx.Connection
  entityTableAdapter.Transaction = tx
  entityTableAdapter.Update(entityRow)
End Sub

Item Save method:

Public Overrides Sub Save()
  // setup the transaction:
  dim cn as SqlConnection = itemTableAdapter.Connection
  cn.Open
  dim tx as Sql Transaction = cn.BeginTransaction
  // save the base object properties:
  MyBase.Save(tx)
  // save this object's properties:
  itemTableAdapter.Connection = cn
  itemTableAdapter.Transaction = tx
  itemTableAdapter.Update(itemRow)
  // commit the transaction
  tx.Commit
End Sub

Now when I attempt to save a new Item everything works up to the itemTableAdapter.Update(itemRow) where I get an error similar to

INSERT statement conflicted with the
FOREIGN KEY constraint… column
‘EntityID’

Indeed, the SQL table does not contain a row in the Entity table with the necessary ID at this point because the transaction is not complete.

Am I going about this the wrong way? My goal is to prevent the Entity record from being inserted if inserting the Item record fails. I could write code to delete the Entity record again, but this does not seem very elegant to me…

The code above has been greatly simplified to illustrate the problem. I am happy to post more details if it helps.

Thanks in advance for any help!

JE

  • 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-18T02:34:39+00:00Added an answer on May 18, 2026 at 2:34 am

    The way to handle situations like this in some databases (e.g. Oracle, PostgreSQL) would be to use deferrable contraints. Unfortunately it appears that SQL Server does not as yet support deferrable constraints. About the only thing I can think of is to add logic to your app to delete the unnecessary Entity if the Item insert fails for some other reason.

    Share and enjoy.

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

Sidebar

Related Questions

First, a little background: I'm displaying a data set with 288 rows and 8
First a little intro: Last year i wrote this http://dragan.yourtree.org/code/canvas-3d-graph/ Now, i want to
First question on here so please be nice :) I know very little about
I know this question might sound a little cheesy but this is the first
OK, having tried my first TDD attempt, it's time to reflect a little and
First off, I am using Windows XP. I have multiple hard drives and it
First of all, I know how to build a Java application. But I have
First, let's get the security considerations out of the way. I'm using simple authentication
First off, I understand the reasons why an interface or abstract class (in the
First off if you're unaware, samba or smb == Windows file sharing, \\computer\share etc.

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.