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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:40:41+00:00 2026-06-12T14:40:41+00:00

Set the primary key to identity in database (SQL Server) When using EF to

  • 0

Set the primary key to identity in database (SQL Server)

When using EF to insert data I am getting error

Cannot insert explicit value for identity column in table ‘myTable’
when IDENTITY_INSERT is set to OFF

with this particular entity, though I am using the same approach with other entities to insert the data and getting no error with them.

   public class MyTable 
    {
    [Key/*,DatabaseGenerated(DatabaseGeneratedOption.Identity)*/]
    public int ID{get;set;}
    public string name {get;set;}
    public string type {get;set;}
    public string status {get;set;}
    public int country {get;set;}
    public DateTime start_date {get;set;}
    public DateTime due_date {get;set;}
    }`

Query

    MyTable check= new MyTable();
            check.name = checkName;
            check.type = "DB";
            check.status = "New";
            check.country = country;
            check.start_date = DateTime.Now;
            check.due_date = dueDate;
            db.myTables.Add(check);
            db.SaveChanges();

Updated :

After uncommenting the above line

(DatabaseGenerated(DatabaseGeneratedOption.Identity)) 

it’s throwing an exception

A dependent property in a ReferentialConstraint is mapped to a
store-generated column. Column: ‘ID’

  • 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-12T14:40:43+00:00Added an answer on June 12, 2026 at 2:40 pm

    I am not sure if it is in your case too….but the second error says..you are using your primary key as foreign key in some relation which is only valid for property having DatabaseGeneratedOption.None .

    So to use your primary key as identity you can decorate it with DatabaseGeneratedOption.Identity

    and in that case it will throw an exception because you can not use identity for relation(use DatabaseGeneratedOption.None instead).

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

Sidebar

Related Questions

My table Sections (SQL Server) has ID as a primary key (int, identity) and
If I set a database column as an autonumber primary key field, will the
I am using SQL Server 2008 Enterprise. I have tried that if I set
While updating a DataTable to a SQL Server database I get the error message
My database has a parent table with an auto-incrementing primary key identity 'ID', and
Is there a way to set the PRIMARY KEY in a single CREATE TABLE
We would like to reserve a set of primary key identifiers for all tables
I created a table tbl_Candidate , but I forgot to set the primary key
I have a primary key set up to auto increment. I am doing multiple
So, I'm using Entity Framework and need to now how set my own primary

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.