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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:59:51+00:00 2026-05-23T13:59:51+00:00

I’m inserting a number of instances of a particular object to a database using

  • 0

I’m inserting a number of instances of a particular object to a database using the Entity Framework. I have a single object context to which I attach several ‘Product’ objects. The object I insert has no relationship to any other table in the database. It is purely a standalone entity. I am using the ‘EFProf’ tool to profile the application.

When I call ‘SaveChanges()’ to persist my ‘Product’ entities to SQL Server I am warned by EFProf that I have the ‘Select N+1’ anti-pattern. I don’t see how this is possible since I am simply inserting. My understanding of ‘Select N+1’ is that it relates to inefficient object retrieval. I am not retrieving anything, only inserting.

When I examine the generated SQL, I see that Entity Framework has generated a select statement on my behalf which returns the Id of the newly inserted object. This select statement is performed for every entity I insert. Could this be the cause of the select N+1 problem? If so, how can I avoid this anti-pattern when inserting a number of entities of the same type in a single call to SaveChanges()?

The generated SQL is below:

insert [dbo].[Products]
  ([ProductName],
   [ProductNum],
   [Price],
   [EntryDate],
   [Description],
   [Category],
   [UnitsInStock])
values('TestProduct' /* @0 */,
   0 /* @1 */,
   0 /* @2 */,
   '2011-07-09T17:14:49.00' /* @3 */,
   'Category: Test Products - Name TestProduct' /* @4 */,
   'Test Products' /* @5 */,
   0 /* @6 */)


select [Id]
from   [dbo].[Products]
where  @@ROWCOUNT > 0
       and [Id] = scope_identity()
  • 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-23T13:59:51+00:00Added an answer on May 23, 2026 at 1:59 pm

    You cannot avoid that additional select. Once you are using autogenerated Ids in the database and your model is correctly configured for them EF will always create this additional select.

    You don’t need to bother. Performance of “bulk insert” with EF is so terribly bad that this additional select during each insert means nothing. What should you deal with is the processing itself because EF will create separate roundtrip to the database for each inserted record and that is the problem. If you really want to pass large number of object into database, you want to do it often (it is not one time job) and you expect some performance you should definitely look for other non-EF solution. For example already mentioned SqlBulkCopy.

    If you just need to insert few instances in some operation you just let it be. That is how EF works.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I have a reasonable size flat file database of text documents mostly saved in
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't

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.