2

在我的模型中,我设置defaultPersistenceEnforce="true"了 ,因为我希望数据库强制执行参照完整性。但是,删除实体时出现错误。

这是我的模型:

<cf:entity name="Keyword">
  <cf:property name="Id" key="true" />
  <cf:property name="Value" typeName="string" />
</cf:entity>

<cf:entity name="BaseOrderline" abstract="true">
  <cf:property name="Id" key="true" />
</cf:entity>

<cf:entity name="Orderline" baseTypeName="{0}.BaseOrderline">
  <cf:property name="Order" typeName="{0}.Order" />
  <cf:property name="Keywords" typeName="{0}.KeywordCollection" />
</cf:entity>

<cf:entity name="Order">
  <cf:property name="Id" key="true" />
  <cf:property name="Keywords" typeName="{0}.KeywordCollection" />
  <cf:property name="Orderlines" typeName="{0}.OrderlineCollection" cascadeDelete="Before" cascadeSave="After" />
</cf:entity>  

这是我的代码:

Keyword keyword = new Keyword();
keyword.Save();

Order order = new Order();
order.Keywords.Add(keyword);

Orderline orderline = new Orderline();
orderline.Keywords.Add(keyword);
order.Orderlines.Add(orderline);

// Save the order and its order lines.
order.Save();

// Delete the order and its order lines.
order.Delete();

我收到以下错误:

The DELETE statement conflicted with the REFERENCE constraint "FK_Orl_Bas_Bas_Ore". The conflict occurred in table "dbo.Orderline_Keywords_Keyword", column 'BaseOrderline_Id'.
The DELETE statement conflicted with the REFERENCE constraint "FK_Ore_Ord_Ord_Ord". The conflict occurred in table "dbo.Orderline", column 'Orderline_Order_Id'.
Concurrency error in procedure Order_Delete

orderline.Keywords.Add(keyword);如果我从代码中删除,一切都会好起来的。

下面是存储过程 Order_Delete 的代码:

USE [PersistenceIdentity]
GO
/****** Object:  StoredProcedure [dbo].[Order_Delete]    Script Date: 18-7-2016 20:41:27 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Order_Delete]
(
 @Order_Id [uniqueidentifier],
 @_rowVersion [rowversion]
)
AS
SET NOCOUNT ON
DECLARE @error int, @rowcount int
DECLARE @tran bit; SELECT @tran = 0
IF @@TRANCOUNT = 0
BEGIN
 SELECT @tran = 1
 BEGIN TRANSACTION
END
DELETE [Order_Keywords_Keyword] FROM [Order_Keywords_Keyword] 
    WHERE ([Order_Keywords_Keyword].[Order_Id] = @Order_Id)
SELECT @error = @@ERROR, @rowcount = @@ROWCOUNT
DELETE [BaseOrderline] FROM [BaseOrderline]
    INNER JOIN [Orderline] ON ([BaseOrderline].[BaseOrderline_Id] = [Orderline].[BaseOrderline_Id])
            LEFT OUTER JOIN [Order] ON ([Orderline].[Orderline_Order_Id] = [Order].[Order_Id]) 
    WHERE ([Order].[Order_Id] = @Order_Id)
SELECT @error = @@ERROR, @rowcount = @@ROWCOUNT
DELETE [Orderline] FROM [Orderline]
    LEFT OUTER JOIN [Order] ON ([Orderline].[Orderline_Order_Id] = [Order].[Order_Id])
            LEFT OUTER JOIN [Orderline] [Orderline$1] ON ([Order].[Order_Id] = [Orderline$1].[Orderline_Order_Id]) 
    WHERE ([Order].[Order_Id] = @Order_Id)
SELECT @error = @@ERROR, @rowcount = @@ROWCOUNT
DELETE [Order] FROM [Order] 
    WHERE (([Order].[Order_Id] = @Order_Id) AND ([Order].[_rowVersion] = @_rowVersion))
SELECT @error = @@ERROR, @rowcount = @@ROWCOUNT
IF(@rowcount = 0)
BEGIN
    IF @tran = 1 ROLLBACK TRANSACTION
    RAISERROR (50001, 16, 1, 'Order_Delete')
    RETURN
END
IF @tran = 1 COMMIT TRANSACTION

RETURN

如您所见,此存储过程中缺少以下行:

DELETE [Orderline_Keywords_Keyword] FROM [Orderline_Keywords_Keyword] 
    WHERE ([Orderline_Keywords_Keyword].[Orderline_Id] = @Orderline_Id)
4

0 回答 0