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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:48:21+00:00 2026-05-27T16:48:21+00:00

There are existing options for doing bulk insert into a single table with EF

  • 0

There are existing options for doing bulk insert into a single table with EF entities. Specifically, this SO question and using this class from David Browne.

In the case of trying to bulk insert rows into both a parent and child table, however, nothing jumps out as an option at that same level of convenience.

The ‘hacks’ I can think of (but I’m hoping there’s at least one better option out there) include:

  1. generate the PK’s and set the FK’s before insert (in this scenario, we know nothing else is inserting at the same time), then do the bulk inserts of both (turning off IDENTITY_INSERT during the parent insert if necessary)
  2. bulk insert (using the linked SO question’s approach) the parent rows, select them (enough columns to identify which parent row is which), generate child rows, bulk insert those
  3. generate the sql necessary to insert all the rows in a single batch, doing each parent and then all related children, using @@identity to fill in the FK for the child inserts

The ‘pregenerate PK values’ approach (I haven’t actually tried it) seems fine, but is fragile (requires no other inserts to at least parent during the operation) and depends on either an empty table or selecting max(pk)+1 beforehand.

Since SqlBulkCopy seems to be built around inserting a table at a time (like bcp), anything that still lets sql generate the PK/identity column would seem to be built around ‘dropping down’ to ado.net and building the sql.

Is there an option outside of ‘generate the tons of sql’ that I’m missing? If not, is there something out there that already generates the sql for mass-insert into related tables?

Thanks!!

  • 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-27T16:48:21+00:00Added an answer on May 27, 2026 at 4:48 pm

    The first rule of any foreign key constraint is that it must exist, as a primary key or unique constraint, in another table before inserted into the foreign key table.

    This works great when you are adding a few rows at a time (traditional transaction processing environment). Howevere, you are trying to bulk insert into both at the same time. I’d term this as batch processing. Basically, the bulk update lock on the parent table is going to block the child table from reading it to check that the fk linkage is valid.

    I’d say your 2 options would be 1.) leave the fk out entirely or 2.) Set the fk as nocheck before the bulk insert, then turn the check on after the bulk insert is complete with an alter table.

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

Sidebar

Related Questions

First, I'm not sure if I should break this question into smaller, individual questions.
Are there existing template extract libraries in either python or php? Perl has Template::Extract
Is there existing software for discriminative reranking, such as that used by the Charniak
Is there an existing application or library in Java which will allow me to
Is there an existing solution to create a regular expressions dynamically out of a
Are there any existing solutions for remote execution of commands on a windows server
Is there any existing code in Apache HttpClient or in the servlet API to
Is there an existing PL/SQL method which takes a string and returns the same
Are there any existing libraries in existence that will parse a datetime from a
Is there an existing algorithm for converting a quaternion representation of a rotation to

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.