下表在SQL中工作,我的意思是我可以添加行,如果有重复,它会提示我无法插入。尝试在Entity Framework中插入时,数据确实会添加到本地存储中,命令为:
tblOrderDetail ord = new tblOrderDetail();
ord.FK_ServiceID = cboService.ToSelectedIntValue();
ord.FK_QuoteID = primKey;
ord.Quantity = txtServiceQuant.ToInt();
ord.LineTotal = txtServiceQuant.ToInt() * txtServiceUnit.ToDecimal();
contx.tblOrderDetails.Add(ord);
如果我尝试保存更改,如下所示,如果它是一个独特的行,一切都很完美。 此外,如果我尝试保存已存在的行,我会得到预期的唯一约束错误。
在此之后,如果我尝试插入另一个唯一的订单详细信息行,我会得到一个未提交给DB的PREVIOUS行的唯一约束错误。
这是因为本地商店还有副本吗?或者我错过了什么?
contx.SaveChanges();
保存更改时发生以下错误:
CREATE TABLE [dbo].[tblOrderDetail](
[PK_OrderDetailID] [int] IDENTITY(1,1) Primary Key NOT NULL,
[FK_ServiceID] [int] NULL,
[FK_ProductID] [int] NULL,
[FK_QuoteID] [int] NULL,
[FK_InvoiceID] [int] NULL,
[Quantity] [money] NOT NULL,
[LineTotal] [money] NOT NULL,
[Deleted] [bit] NULL,
CONSTRAINT [uc_InvoiceQuoteProductService] UNIQUE NONCLUSTERED
(
[FK_InvoiceID] ASC,
[FK_QuoteID] ASC,
[FK_ServiceID] ASC,
[FK_ProductID] ASC
))
GO
Go
ALTER TABLE [dbo].[tblOrderDetail] ADD CONSTRAINT [DF_tblOrderDetail_Deleted] DEFAULT ((0)) FOR [Deleted]
GO
ALTER TABLE [dbo].[tblOrderDetail] WITH CHECK ADD FOREIGN KEY([FK_InvoiceID])
REFERENCES [dbo].[tblInvoice] ([PK_InvoiceID])
GO
ALTER TABLE [dbo].[tblOrderDetail] WITH CHECK ADD FOREIGN KEY([FK_QuoteID])
REFERENCES [dbo].[tblQuote] ([PK_QuoteID])
GO
ALTER TABLE [dbo].[tblOrderDetail] WITH CHECK ADD FOREIGN KEY([FK_ServiceID])
REFERENCES [dbo].[tblService] ([PK_ServiceID])
GO
SET IDENTITY_INSERT [dbo].[tblOrderDetail] ON
INSERT [dbo].[tblOrderDetail] ([PK_OrderDetailID], [FK_ServiceID], [FK_ProductID], [FK_QuoteID], [FK_InvoiceID], [Quantity], [LineTotal], [Deleted]) VALUES (1,1, NULL, 1, NULL, 12.0000, 3000.0000, NULL)
INSERT [dbo].[tblOrderDetail] ([PK_OrderDetailID], [FK_ServiceID], [FK_ProductID], [FK_QuoteID], [FK_InvoiceID], [Quantity], [LineTotal], [Deleted]) VALUES (2,2, NULL, 2, NULL, 1.0000, 300.0000, NULL)
INSERT [dbo].[tblOrderDetail] ([PK_OrderDetailID], [FK_ServiceID], [FK_ProductID], [FK_QuoteID], [FK_InvoiceID], [Quantity], [LineTotal], [Deleted]) VALUES (3,3, NULL, 5, NULL, 4.0000, 600.0000, NULL)
INSERT [dbo].[tblOrderDetail] ([PK_OrderDetailID], [FK_ServiceID], [FK_ProductID], [FK_QuoteID], [FK_InvoiceID], [Quantity], [LineTotal], [Deleted]) VALUES (4,4, NULL, 5, NULL, 8.0000, 4000.0000, NULL)
INSERT [dbo].[tblOrderDetail] ([PK_OrderDetailID], [FK_ServiceID], [FK_ProductID], [FK_QuoteID], [FK_InvoiceID], [Quantity], [LineTotal], [Deleted]) VALUES (5,5, NULL, 5, NULL, 6.0000, 2700.0000, NULL)
SET IDENTITY_INSERT [dbo].[tblOrderDetail] OFF
内在消息如下:
{“违反UNIQUE KEY约束'uc_InvoiceQuoteProductService'。无法在对象'dbo.tblOrderDetail'中插入重复键。重复键值为(,4,2,)。\ r \ n语句已终止。”}
堆栈跟踪:
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()
at WWITIQ.PresentationLayer.Windows.pgeQuotes.btnAddServices_Click_1(Object sender, RoutedEventArgs e) in c:\Users\...
答案 0 :(得分:1)
如果您仍然在"同一"上下文,在尝试再次保存之前,您必须首先将有问题的实体从上下文中分离出来。
如果您没有,它仍将附加到上下文中,SaveChanges
将应用于所有附加实体。
所以尝试做类似的事情:
contx.Entry(<theEntityWhichBreaksUnicityConstraint>).State = EntityState.Detached;