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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:51:18+00:00 2026-06-09T15:51:18+00:00

On occasion, I need to insert a specific ID as the primary key of

  • 0

On occasion, I need to insert a specific ID as the primary key of an entity I’m adding to my data. In classic MS SQL, you can do this by calling SET IDENTITY_INSERT tablename ON before calling the INSERT INTO statement. I cannot seem to figure out a way to do the same using EF 4.1 Code First.

Dim specificIdRecord As New Record() With {
    .RecordID = 25,
    'other settings here
}

myDbContext.Records.Add(specificIdRecord)
myDbContext.SaveChanges()

specificIdRecord.RecordID '<== this does not always equal 25!

There’s also no error being thrown to tell me it’s ignoring my RecordID and automatically generating one.

NOTE: I do not want to modify my model or modify my context as this is only intended for occasional insert operations. Also, I did see a possible solution using ExecuteStoreCommand to manually set the IDENTITY_INSERT before and after the insert runs, but that will require three transactions AFAIK, I want EF to wrap this all up into one.

  • 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-09T15:51:20+00:00Added an answer on June 9, 2026 at 3:51 pm

    EF doesn’t support this. If you use IDENTITY columns in the database your mapping contains this information and you can never send a key value to the database (EF will automatically skip it).

    The best option in this case is using ExecuteStoreCommand for both setting IDENTITY_INSERT and INSERT SQL commands.

    Any workaround based on EF would require two different EF mappings: one with key set as database generated and one without this setting. You will use the first mapping for common processing and the second mapping when you want to insert explicit key values. Using two mappings means using two different EDMX files or two different DbModelBuilder instances (two context types).

    Once you will have two mappings you will still have to solve the problem with turning IDENTITY_INSERT on and off. This will require you to create custom provider wrapper to attach those SQL commands to each generatedINSERT command.

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

Sidebar

Related Questions

I have a set of data which in some occasion I need to sort
I have occasion to need to print code (Horrors!!! ;) ), and i was
on more than one occasion I felt the need to define class methods that
I'm using PHP to data scrape another website. However, on certain occasions I need
I am using svn and on occasion I need to revert some changes that
I have a single page that I need to on occasion asynchronously check the
I need to apply some xml templates to various streams of xml data (and
How can I go about storing a vb.net user defined object in a sql
I need to deliver specific messages to the user based on its proximity to
I have a table full of data that's typically pretty manageable, but on occasion

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.