获取'当IDENTITY_INSERT设置为OFF时,无法在表'OrderPromo'中为标识列插入显式值。'在没有身份的桌子上

时间:2017-01-05 18:52:04

标签: c# sql-server asp.net-mvc entity-framework

我创建了这样的表:

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]

有趣的实体看起来像这样:

[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; }

}

[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; }
    }

当我尝试将数据插入数据库时​​出现问题。我收到了Cannot insert explicit value for identity column in table 'OrderPromo' when IDENTITY_INSERT is set to OFF.

但是,OrderPromo表中没有Identity列。我甚至试图将IDENTITY_INSERT设置为ON只是为了100%确定,并且SQL给出错误,表明表上没有Identity列。

谢谢。

编辑 -

订单的表定义:

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]

2 个答案:

答案 0 :(得分:0)

您可以尝试将IDENTITY_INSERT打开,如

SET IDENTITY_INSERT Order ON
--Insert script here
SET IDENTITY_INSERT Order OFF

答案 1 :(得分:0)

您声明OrderId是表OrderPromo中的主键,尽管它似乎是一个外键。它应该是表Order的主键。

从OrderPromo中删除OrderId上的主键,它应该可以正常工作。

我相信正在发生的事情是插件试图将引用的Order的值强制到OrderPromo的OrderId字段中,导致它失败。