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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:14:38+00:00 2026-06-10T19:14:38+00:00

I am working on application where I use EF code first (v4.4) as ORM

  • 0

I am working on application where I use EF code first (v4.4) as ORM and SqlServer as database. I also need an option to import data from another database type, which has similar data structure. During the data transfer I need to transfer identity columns with values they have, in order not to break related data.

Assuming this is the sequence of operations:

  1. fetch the data
  2. convert it to fit EF model
  3. add each entity to DbContext
  4. save changes

the problem is that it will not insert identity values as they were, due to the fact that my primary keys are set as Identity columns.

One of the ways to solve this problem is to use IDENTITY_INSERT command, but it requires to be executed together with insert queries under the same connection cycle. By connection cycle I mean that connection must not be closed during all operations (IDENTITY_INSERT and INSERT INTO).

Problem: ExecuteSqlCommand closes the connection, even if it was explicitly opened prior calling it.

A solution to this problem is to not use EF for this task, but go with ADO.NET SqlCommand. This way we have more control of when the connection will be opened / closed.

Problem: if I change my model, I also need to change the import queries, so I need to maintain database structure on two places.

Any advice how to achieve this with EF? I do not have the slightest idea why did EF team decide to always close the connection after calling ExecuteSqlCommand.

  • 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-06-10T19:14:39+00:00Added an answer on June 10, 2026 at 7:14 pm

    In order to go with EF DbContext, here is what needs to be done:

    • Use IObjectContextAdapter to open the connection
    • Use ObjectContext’s ExecuteStoreCommand to turn IDENTITY_INSERT ON/OFF
    • In the DbContext have a constructor where you have a switch to turn off DatabaseGeneratedOption.Identity for PKs when doing data conversion

    As for the perfromance, the best way I found is to do a batch update for X records, and then dispose / recreate the Context. Also, set AutoDetectChangesEnabled to false

    Interesting reading about performance can be found here:

    Fastest Way of Inserting in Entity Framework

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

Sidebar

Related Questions

I currently working on a application that use Entity Framework 4.1 (code first) and
I'm building a MVC 3 application and use Entity Framework 4.3 code-first. My context
I'm working on a 2-tier WPF/EF Code First application. I did a lot of
I am working on game application and i use AnimationDrawable for image change. but
I am working on web application that will use PHP & MySQL. Application will
I'm working on a project to convert an existing Java web application to use
I'm currently studying socket programming, i'm working on creating an application to use my
I'm working on an iPhone application which will use long-polling to send event notifications
I have am working on a web application that makes use of helper classes.
An application i am working on requires the use of a JList where each

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.