Entity Framework 当 IDENTITY_INSERT 设置为 OFF 时,多对多插入抛出错误无法在 table 中插入标识列的显式值
Entity Framework Many-to-Many Insert throws error Cannot insert explicit value for identity column in table when IDENTITY_INSERT is set to OFF
我创建了一个从数据库创建的代码优先模型,所以我有我的实体和上下文的所有设置。
这是有问题的 table 的图表。
Many to Many relation diagram
如果有意义的话,关系是为了知道哪个付款涵盖了哪些订单。
我试图通过将 SalesOrder 实例添加到 EF 中的 payment.SalesOrders 来向 PaymentSalesOrder 添加记录。
调用 SaveChanges() 时出现此错误:
ErrorCode
Cannot insert explicit value for identity column in table 'PaymentSalesOrder' >when IDENTITY_INSERT is set to OFF.
这是代码,基本上我查询当前客户的数据库并包括他的付款和订单。
我将付款分配给销售订单并更改付款和销售订单以反映它们的可用性。
最后我将 salesOrder 添加到付款中并调用 SaveChanges()。
我有一个客户在数据库中有一份订单和一笔付款,因此此代码中的所有实体都填充了正确的数据。
Code
Using db As New eRestaurantEF.Context
If _customer Is Nothing Then
Exit Sub
End If
Dim tmpCustomer As eRestaurantEF.Customer
tmpCustomer = (From tmp In db.Customers.Include("Payments").Include("SalesOrders")
Where tmp.customerID = _customer.customerID
Select tmp).FirstOrDefault
For Each paymentRow As eRestaurantEF.Payment In tmpCustomer.Payments
If paymentRow.PaymentRemainder > 0 Then
For Each orderRow As eRestaurantEF.SalesOrder In tmpCustomer.SalesOrders
db.SalesOrders.Attach(orderRow)
db.Entry(orderRow).State = Entity.EntityState.Unchanged
If paymentRow.PaymentRemainder > 0 Then
If Not orderRow.FullyPaid Then
If paymentRow.PaymentRemainder >= orderRow.Remainder Then
Dim remainder As Decimal
remainder = paymentRow.PaymentRemainder
remainder -= orderRow.Remainder
paymentRow.PaymentRemainder = remainder
orderRow.Remainder = 0
orderRow.IsPaid = True
orderRow.IsCredit = True
orderRow.FullyPaid = True
paymentRow.SalesOrders.Add(orderRow)
ElseIf paymentRow.PaymentRemainder <= orderRow.Total Then
Dim remainder As Decimal
remainder = paymentRow.PaymentRemainder
remainder -= orderRow.Remainder
If remainder = 0 Then
paymentRow.PaymentRemainder = 0
orderRow.Remainder = 0
orderRow.IsPaid = True
orderRow.IsCredit = True
orderRow.FullyPaid = True
paymentRow.SalesOrders.Add(orderRow)
Exit For
ElseIf remainder < 0 Then
paymentRow.PaymentRemainder = 0
orderRow.Remainder = Math.Abs(remainder)
orderRow.IsPaid = False
orderRow.IsCredit = True
orderRow.FullyPaid = False
orderRow.Payments.Add(paymentRow)
Exit For
End If
End If
End If
End If
Next 'OrdersList
End If
Next 'PaymentsList
db.SaveChanges()
End Using
这是关系 table:
的 Fluent Api
modelBuilder.Entity(Of Payment)() _
.HasMany(Function(e) e.SalesOrders) _
.WithMany(Function(e) e.Payments) _
.Map(Function(m) m.ToTable("PaymentSalesOrder").MapLeftKey("Payment_ID").MapRightKey("SalesOrder_No"))
PaymentSalesOrder table 中的两个 FK 都是在数据库中创建的复合键的一部分,以确保没有重复的行。
我什至尝试删除密钥并将其更改为唯一索引,但问题仍然存在。
这是来自 Sql 的 table 脚本:
CREATE TABLE [dbo].[PaymentSalesOrder](
[Payment_ID] [int] IDENTITY(1,1) NOT NULL, <---- Here is the culprit remove the identity from this column
[SalesOrder_No] [int] NOT NULL,
CONSTRAINT [PK_PaymentSalesOrder] PRIMARY KEY CLUSTERED
(
[Payment_ID] ASC,
[SalesOrder_No] 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].[PaymentSalesOrder] WITH CHECK ADD CONSTRAINT [FK_PaymentSalesOrder_Payment] FOREIGN KEY([Payment_ID])
REFERENCES [dbo].[Payment] ([PaymentID])
GO
ALTER TABLE [dbo].[PaymentSalesOrder] CHECK CONSTRAINT [FK_PaymentSalesOrder_Payment]
GO
ALTER TABLE [dbo].[PaymentSalesOrder] WITH CHECK ADD CONSTRAINT [FK_PaymentSalesOrder_SalesOrder] FOREIGN KEY([SalesOrder_No])
REFERENCES [dbo].[SalesOrder] ([SalesOrderNo])
GO
ALTER TABLE [dbo].[PaymentSalesOrder] CHECK CONSTRAINT [FK_PaymentSalesOrder_SalesOrder]
GO
很抱歉拖了这么久 post 但这真的很让人抓狂,我看不出问题出在哪里。
我正在考虑把多对多的关系全部去掉,但不知道原因就放弃对我来说并不容易,如果我再次遇到它怎么办。
错误清楚地表明您正试图在 IDENTITY
列中插入一个值,但您不能将值插入 IDENTITY
列,在您的情况下是 Payment_ID
PaymentSalesOrder
table。 IDENTITY
当您通过增加提供的 INCREMENT
值插入新行时,列自动生成其值,在您的情况下是 1
。我的意思是,每当您插入新行时,Payment_ID
的值将从 1
开始自动插入,并且会为每个新行递增 1。
您可以获得有关 IDENTITY here 的更多信息。
希望你现在清楚了
我创建了一个从数据库创建的代码优先模型,所以我有我的实体和上下文的所有设置。 这是有问题的 table 的图表。 Many to Many relation diagram 如果有意义的话,关系是为了知道哪个付款涵盖了哪些订单。 我试图通过将 SalesOrder 实例添加到 EF 中的 payment.SalesOrders 来向 PaymentSalesOrder 添加记录。 调用 SaveChanges() 时出现此错误:
ErrorCode
Cannot insert explicit value for identity column in table 'PaymentSalesOrder' >when IDENTITY_INSERT is set to OFF.
这是代码,基本上我查询当前客户的数据库并包括他的付款和订单。 我将付款分配给销售订单并更改付款和销售订单以反映它们的可用性。 最后我将 salesOrder 添加到付款中并调用 SaveChanges()。
我有一个客户在数据库中有一份订单和一笔付款,因此此代码中的所有实体都填充了正确的数据。
Code
Using db As New eRestaurantEF.Context
If _customer Is Nothing Then
Exit Sub
End If
Dim tmpCustomer As eRestaurantEF.Customer
tmpCustomer = (From tmp In db.Customers.Include("Payments").Include("SalesOrders")
Where tmp.customerID = _customer.customerID
Select tmp).FirstOrDefault
For Each paymentRow As eRestaurantEF.Payment In tmpCustomer.Payments
If paymentRow.PaymentRemainder > 0 Then
For Each orderRow As eRestaurantEF.SalesOrder In tmpCustomer.SalesOrders
db.SalesOrders.Attach(orderRow)
db.Entry(orderRow).State = Entity.EntityState.Unchanged
If paymentRow.PaymentRemainder > 0 Then
If Not orderRow.FullyPaid Then
If paymentRow.PaymentRemainder >= orderRow.Remainder Then
Dim remainder As Decimal
remainder = paymentRow.PaymentRemainder
remainder -= orderRow.Remainder
paymentRow.PaymentRemainder = remainder
orderRow.Remainder = 0
orderRow.IsPaid = True
orderRow.IsCredit = True
orderRow.FullyPaid = True
paymentRow.SalesOrders.Add(orderRow)
ElseIf paymentRow.PaymentRemainder <= orderRow.Total Then
Dim remainder As Decimal
remainder = paymentRow.PaymentRemainder
remainder -= orderRow.Remainder
If remainder = 0 Then
paymentRow.PaymentRemainder = 0
orderRow.Remainder = 0
orderRow.IsPaid = True
orderRow.IsCredit = True
orderRow.FullyPaid = True
paymentRow.SalesOrders.Add(orderRow)
Exit For
ElseIf remainder < 0 Then
paymentRow.PaymentRemainder = 0
orderRow.Remainder = Math.Abs(remainder)
orderRow.IsPaid = False
orderRow.IsCredit = True
orderRow.FullyPaid = False
orderRow.Payments.Add(paymentRow)
Exit For
End If
End If
End If
End If
Next 'OrdersList
End If
Next 'PaymentsList
db.SaveChanges()
End Using
这是关系 table:
的 Fluent ApimodelBuilder.Entity(Of Payment)() _
.HasMany(Function(e) e.SalesOrders) _
.WithMany(Function(e) e.Payments) _
.Map(Function(m) m.ToTable("PaymentSalesOrder").MapLeftKey("Payment_ID").MapRightKey("SalesOrder_No"))
PaymentSalesOrder table 中的两个 FK 都是在数据库中创建的复合键的一部分,以确保没有重复的行。 我什至尝试删除密钥并将其更改为唯一索引,但问题仍然存在。 这是来自 Sql 的 table 脚本:
CREATE TABLE [dbo].[PaymentSalesOrder](
[Payment_ID] [int] IDENTITY(1,1) NOT NULL, <---- Here is the culprit remove the identity from this column
[SalesOrder_No] [int] NOT NULL,
CONSTRAINT [PK_PaymentSalesOrder] PRIMARY KEY CLUSTERED
(
[Payment_ID] ASC,
[SalesOrder_No] 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].[PaymentSalesOrder] WITH CHECK ADD CONSTRAINT [FK_PaymentSalesOrder_Payment] FOREIGN KEY([Payment_ID])
REFERENCES [dbo].[Payment] ([PaymentID])
GO
ALTER TABLE [dbo].[PaymentSalesOrder] CHECK CONSTRAINT [FK_PaymentSalesOrder_Payment]
GO
ALTER TABLE [dbo].[PaymentSalesOrder] WITH CHECK ADD CONSTRAINT [FK_PaymentSalesOrder_SalesOrder] FOREIGN KEY([SalesOrder_No])
REFERENCES [dbo].[SalesOrder] ([SalesOrderNo])
GO
ALTER TABLE [dbo].[PaymentSalesOrder] CHECK CONSTRAINT [FK_PaymentSalesOrder_SalesOrder]
GO
很抱歉拖了这么久 post 但这真的很让人抓狂,我看不出问题出在哪里。 我正在考虑把多对多的关系全部去掉,但不知道原因就放弃对我来说并不容易,如果我再次遇到它怎么办。
错误清楚地表明您正试图在 IDENTITY
列中插入一个值,但您不能将值插入 IDENTITY
列,在您的情况下是 Payment_ID
PaymentSalesOrder
table。 IDENTITY
当您通过增加提供的 INCREMENT
值插入新行时,列自动生成其值,在您的情况下是 1
。我的意思是,每当您插入新行时,Payment_ID
的值将从 1
开始自动插入,并且会为每个新行递增 1。
您可以获得有关 IDENTITY here 的更多信息。
希望你现在清楚了