[英]Getting 'Cannot insert explicit value for identity column in table 'OrderPromo' when IDENTITY_INSERT is set to OFF.' on table with NO Identity
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.
Cannot insert explicit value for identity column in table 'OrderPromo' when IDENTITY_INSERT is set to OFF.
收到“ 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. 但是,OrderPromo表中没有“标识”列。 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. 我什至尝试将IDENTITY_INSERT设置为ON只是为了100%保证,并且SQL给出错误,指示表上没有Identity列。
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]
You can try turn the IDENTITY_INSERT On like 您可以像这样尝试打开IDENTITY_INSERT
SET IDENTITY_INSERT Order ON
--Insert script here
SET IDENTITY_INSERT Order OFF
You are declaring OrderId to be the primary key in table OrderPromo although it seems to be a foreign key. 您在表OrderPromo中声明OrderId为主键,尽管它似乎是外键。 It should be the primary key of table Order. 它应该是表Order的主键。
Remove the primary key on OrderId from OrderPromo and it should work. 从OrderPromo中删除OrderId上的主键,它应该可以工作。
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. 我相信发生的事情是该插入试图将引用的Order的值强制插入OrderPromo的OrderId字段中,从而导致失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.