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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:13:48+00:00 2026-06-06T15:13:48+00:00

I am trying to get identifiying relationships to work in Entity Framework so I

  • 0

I am trying to get identifiying relationships to work in Entity Framework so I can remove items from their collection using Remove.

I have created a test like:

public class OrderLine{   
    [Key, Column(Order = 0)]
    public int OrderLineId { get; set; }

    [Key, ForeignKey("Order"), Column(Order = 1)]
    public int OrderId{ get; set; }

    public virtual Order Order{ get; set; }

    [ForeignKey("Product")]
    public int ProductId { get; set; }

    public virtual Product Product { get; set; }

    public int Number { get; set; }}

When I attempt to add a OrderLine like:

        var order=_orderRepository.Find(1);
        var NewOrderLine= new OrderLine()
                                            {
                                                Number = 1,ProductId= 2
                                            };
        order.OrderLines.Add(NewOrderLine);
        _orderRepository.InsertOrUpdate(order);
        _orderRepository.Save();

I get the error

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

When I look at the table SQL has a primary key over OrderId and OrderLineId. Also only OrderLineId is set as identity. So it must be trying to insert a value but the only value it seems to be trying to insert is 0.

Any ideas I can do an insert to the table using:

INSERT INTO [OrderLines]
           ([ProductId ]
           ,[Number]
           ,[OrderId])
     VALUES
           (2
           ,1
           ,1)
  • 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-06T15:13:51+00:00Added an answer on June 6, 2026 at 3:13 pm

    I believe the exception means that one of the composite primary key columns in table OrderLines is an autogenerated identity in the database (I guess the column OrderLineId) but EF does send a value (0 because you don’t specify the property when you create the new OrderLine) for this column in the INSERT statement to the database because the property is not marked as identity in the EF model. By default (= IDENTITY_INSERT is OFF) SQL Server does not allow to insert a value into an identity column explicitly.

    Indeed in your mapping the key columns are not identities because by default in a composite key no property is an identity.

    I think, the solution is to either turn off the identity specification in the database or mark the property as identity in the model:

    public class OrderLine
    {
        [Key, Column(Order = 0), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int OrderLineId { get; set; }
    
        [Key, ForeignKey("Order"), Column(Order = 1)]
        public int OrderId { get; set; }
    
        public virtual Order Order { get; set; }
    
        // ...
    }
    

    DatabaseGeneratedOption.Identity means that EF expects that the column value is generated in the database and therefore does not send the value to the DB when you try to insert an new entity.

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

Sidebar

Related Questions

I am trying get Struts 2 and Tiles to work and I am using
Trying to get this expression to work, can someone look at it and tell
I'm trying get bytes from bitmap in blackberry using the next method in Bitmap:
Hi been trying to get this to work for ages, i have a deals
I'm trying get values from a GridView using the following code: foreach (GridViewRow row
I am trying to get some mouse event management to work in IE using
I'm trying get the visible portion of UIImage from an UIImageView . UIImageView takes
I am trying get all html links within a string and replace them using
I am trying get all html links within a string and replace them using
Hi am trying get GPS using network provider and GPS provider. but am getting

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.