0

I have this table created like so:

USE [OrderMore]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[OrderPromo](
    [OrderId] [int] NOT NULL,
    [PromoId] [int] NOT NULL,
    [Created] [datetime] NOT NULL,
 CONSTRAINT [PK_dbo.OrderPromo] PRIMARY KEY CLUSTERED 
(
    [OrderId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[OrderPromo]  WITH CHECK ADD  CONSTRAINT [FK_dbo.OrderPromo_dbo.Order_OrderId] FOREIGN KEY([OrderId])
REFERENCES [dbo].[Order] ([OrderID])
GO
ALTER TABLE [dbo].[OrderPromo] CHECK CONSTRAINT [FK_dbo.OrderPromo_dbo.Order_OrderId]

The interesting entities look like this:

[Table("OrderPromo")]
public class OrderPromo : EntityBase
{
    /// <summary>
    /// 
    /// </summary>
    [Key, ForeignKey("Order")]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int OrderId { get; set; }

    /// <summary>
    /// 
    /// </summary>
    public int PromoId { get; set; }

    /// <summary>
    /// 
    /// </summary>
    public virtual Order Order { get; set; }

}

and:

[Table("Order")]
    public partial class Order : EntityBase
    {
        /// <summary>
        /// 
        /// </summary>
        [Key]
        public int OrderID { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public virtual Transaction Transaction { get; set; }

        /// <summary>
        /// If there is an Promo used on this Order, it will be recorded in the OrderPromo table.
        /// </summary>
        public virtual OrderPromo OrderPromo { get; set; }
    }

The issue comes when I attempt to insert the data into the database. I receive the Cannot insert explicit value for identity column in table 'OrderPromo' when IDENTITY_INSERT is set to OFF.

However, there is no Identity column in the OrderPromo table. I have even tried to set IDENTITY_INSERT to ON just to be 100% sure, and SQL gives the error indicating there is no Identity column on the table.

Thank you.

EDIT--

Table definition for Order:

USE [OrderMore]
GO
/****** Object:  Table [dbo].[Order]    Script Date: 01/05/2017 10:57:19 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Order](
    [OrderID] [int] IDENTITY(1,1) NOT NULL,
    [OrderFormID] [int] NOT NULL,
    [Notes] [nvarchar](250) NULL,
    [Created] [datetime] NOT NULL,
    [TransactionID] [int] NOT NULL,
    [OrderSequenceContext] [int] NOT NULL DEFAULT ((0)),
 CONSTRAINT [PK_dbo.Order] PRIMARY KEY CLUSTERED 
(
    [OrderID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[Order]  WITH CHECK ADD  CONSTRAINT [FK_dbo.Order_dbo.Transaction_TransactionID] FOREIGN KEY([TransactionID])
REFERENCES [dbo].[Transaction] ([TransactionID])
GO
ALTER TABLE [dbo].[Order] CHECK CONSTRAINT [FK_dbo.Order_dbo.Transaction_TransactionID]
OCDDev
  • 307
  • 1
  • 2
  • 12

2 Answers2

0

You can try turn the IDENTITY_INSERT On like

SET IDENTITY_INSERT Order ON
--Insert script here
SET IDENTITY_INSERT Order OFF
Tony Dong
  • 3,213
  • 1
  • 29
  • 32
  • The error is stating the issue is with OrderPromo, not Order itself. I am unable to set `IDENTITY_INSERT` on OrderPromo because there is no `IDENTITY`. Why/how would turning it Order make a difference? – OCDDev Jan 05 '17 at 19:06
  • Could you post OrderPromo definition? Does not make sense to affect OrderPromo if it don't have IDENTITY – Tony Dong Jan 05 '17 at 19:09
  • Why you have PRIMARY KEY CLUSTERED in OrderPromo table for OrderID? Is this one to one relationship with Order table? How about using OrderId combine with PromoId as PRIMARY KEY? – Tony Dong Jan 05 '17 at 19:16
  • @TonyDong That is how EF migrations set it up. OrderPromo is a one to one with Order. – OCDDev Jan 05 '17 at 19:22
  • Could you add OrderPromoID in OrderPromo table and make it as primary key and make OrderID as Foreign Key? – Tony Dong Jan 05 '17 at 19:26
  • @TonyDong That would change the relationship from one to one or zero to one to many, which I assume is incorrect. – juharr Jan 05 '17 at 19:27
  • Yes, but you can make the OrderID in OrderPromo talbe unique index that will it works for one to one or zero – Tony Dong Jan 05 '17 at 19:33
  • Here is a discuss about how to create a real one to one relationship http://stackoverflow.com/questions/10292355/how-do-i-create-a-real-one-to-one-relationship-in-sql-server – Tony Dong Jan 05 '17 at 19:35
0

You are declaring OrderId to be the primary key in table OrderPromo although it seems to be a foreign key. It should be the primary key of table Order.

Remove the primary key on OrderId from OrderPromo and it should work.

I believe what's happening is that the insert is trying to force the value of the referenced Order into the OrderId field of OrderPromo, causing it to fail.

JuanR
  • 7,405
  • 1
  • 19
  • 30
  • That would change the relationship from one to one or zero to one to many. Also EF does not play well with tables that do not have primary keys. – juharr Jan 05 '17 at 19:16
  • OrderID is the PK of the Order table as well. "I believe what's happening is that the insert is trying to force the value of the referenced Order into the OrderId field of OrderPromo"..that is exactly what I think should be happening. – OCDDev Jan 05 '17 at 19:18
  • You should rethink your design then. I imagine an order Id uniquely identifies an order, not an order promo. If your relationship is one to one, you might as well move the fields to the Order table, no? You can solve the issue with EF needing a PK easily by adding your own PK field, which is the recommended way to go anyways. – JuanR Jan 05 '17 at 20:22