实体框架 - 如何找出引起参照完整性错误的原因?

时间:2011-04-14 11:40:09

标签: .net entity-framework

使用实体框架,我的代码使用LINQ来获取需要从数据库中删除的对象集合。根据具体情况,此集合有时相当大 - 30或40个对象。

我继续尝试从相关对象中取消这些对象,以便我可以删除它们,然后我对每个对象调用context.DeleteObject(),最后是context.SaveChanges()。

大多数情况下,这非常有效,特别是当要删除的对象集合非常小时。但是当它很大时,这个代码有时会引用参考完整性并出现错误,特别是:

The DELETE statement conflicted with the REFERENCE constraint 
\"FK_ServiceFeatureSchemaFragmentSchemaFragment_SchemaFragment\". 
The conflict occurred in database \"MyDataBase\", 
table \"dbo.ServiceFeatureSchemaFragmentSchemaFragment\".\r\n
The statement has been terminated. 

所以看起来有时我的对象有额外的约束,我在删除之前无法找到和解耦。

我正在努力的是因为我正在处理一个相对较大的集合,我无法确定哪一个导致这种情况发生。我正在寻找关于如何通过某种方式识别问题对象来推进的建议,例如,有一种方法可以预先确定集合中的对象是否会在您之前抛出这些类型的错误删除它们?还是另一种方法?

感谢收到的建议。

1 个答案:

答案 0 :(得分:0)

在SQL Server中编写存储过程,您可以在其中看到FK / PK关系的影响,而无需额外的EF映射层来混淆事物。此示例使用User的主要配置文件表中的基于整数的ezUserID或ASP.NET成员资格GUID来开始删除。

您可以在定义关系时使用级联删除...

SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO


CREATE PROC [dbo].[ezUser_Delete]
    @ezUserID int = NULL,
    @UserName nvarchar(128) = NULL
AS
BEGIN
    SET NOCOUNT ON

    DECLARE @UserID uniqueidentifier;

    --DECLARE @TranName VARCHAR(20);   --Generally shouldn't use named transactions in stored procedures
    --SELECT @TranName = 'MyTransaction';
    SET XACT_ABORT ON

    IF @ezUserID IS NULL AND @UserName IS NULL RETURN 0

BEGIN TRY
    BEGIN TRANSACTION;

            IF ISNULL(@ezUserID, 0) > 0

                BEGIN
                    SELECT @UserID = UserID FROM ezUsers WHERE ezUserID = @ezUserID
                    SELECT @UserName = UserName FROM aspnet_Users WHERE UserID = @UserID 
                    IF (@UserName IS NULL)
                        BEGIN
                            ROLLBACK TRANSACTION;
                            RETURN -1
                        END
                END

            ELSE IF (LEN(ISNULL(@UserName,'')) > 0)

                BEGIN
                    SELECT @UserID = UserID FROM aspnet_Users WHERE UserName = @UserName
                    SELECT @ezUserID = ezUserID FROM ezUsers WHERE UserID = @UserID  
                    IF (@ezUserID IS NULL)
                        BEGIN
                            ROLLBACK TRANSACTION;
                            RETURN -1
                        END
                END

            ELSE 
                RETURN -1

            SELECT 'DELETE' AS [Command]
                , U.UserID
                , U.UserName
                , RoleName
                , M.Email
                , EU.ezUserID
                , EU.FirstName, EU.LastName
                , CallSign
                , Gender
                , UIR.RoleID

            FROM dbo.aspnet_Users U 
                    LEFT OUTER JOIN dbo.aspnet_Membership M ON U.UserID = M.UserID

                    LEFT OUTER JOIN dbo.ezUsers EU ON U.UserID = EU.UserID      

                    LEFT OUTER JOIN dbo.aspnet_UsersInRoles UIR ON U.UserID = UIR.UserID
                    LEFT OUTER JOIN dbo.aspnet_Roles R ON UIR.RoleID = R.RoleID     
            WHERE U.UserID = @UserID 

            IF @UserID IS NULL
                RETURN -1

            DELETE FROM dbo.ezUsersDating   WHERE ezUserID = @ezUserID  
            DELETE FROM dbo.ezUsersPhysicalIdentity WHERE ezUserID = @ezUserID

            DELETE FROM dbo.ezMessages WHERE AuthorID = @ezUserID

            DELETE FROM dbo.ezTheUsual WHERE ezUserID = @ezUserID
            DELETE FROM dbo.ezRentals WHERE ezUserID = @ezUserID

            DELETE FROM dbo.ezUsersSharedActivities WHERE ezUserID = @ezUserID

            DELETE FROM dbo.ezCurrentVehicle WHERE UVID IN (SELECT UVID FROM dbo.ezUsersVehicles WHERE ezUserID = @ezUserID)
            DELETE FROM dbo.ezDriverVehicles WHERE ezUserID = @ezUserID  -- Delete this Table
            DELETE FROM dbo.ezOwnerVehicles WHERE OwnerID = @ezUserID   
            DELETE FROM dbo.ezUsersVehicles WHERE ezUserID = @ezUserID

            DELETE FROM dbo.ezPL8Messages 
                WHERE PL8ID IN (
                SELECT PL8ID FROM dbo.ezPL8s WHERE VehicleID IN (SELECT VehicleID FROM dbo.ezVehicles WHERE CreatedBy = @ezUserID))

            DELETE FROM dbo.ezPL8s WHERE VehicleID IN (SELECT VehicleID FROM dbo.ezVehicles WHERE CreatedBy = @ezUserID)

            DELETE FROM dbo.ezPL8s_Vehicles WHERE VehicleID IN (SELECT VehicleID FROM dbo.ezVehicles WHERE CreatedBy = @ezUserID)

            DELETE FROM dbo.ezVehicles WHERE CreatedBy = @ezUserID

            DELETE FROM dbo.ezImages_Users
            WHERE ImageID IN (SELECT ImageID FROM dbo.ezImages 
                                WHERE UploadedBy = @ezUserID)

            DELETE FROM dbo.ezImages_Users
            WHERE ezUserID = @ezUserID

            DELETE FROM dbo.ezImages 
            WHERE UploadedBy = @ezUserID

            DELETE FROM dbo.ezPets
            WHERE ezUserID = @ezUserID

            DELETE FROM dbo.ezAccounts
            WHERE CPID IN (SELECT CPID FROM dbo.ezCompany_People WHERE ezUserID = @ezUserID)

            DELETE FROM dbo.ezCompany_People
            WHERE ezUserID = @ezUserID          

            DELETE FROM dbo.ezMedicals
            WHERE ezUserID = @ezUserID

            DELETE FROM dbo.ezVehicles WHERE CreatedBy = @ezUserID

            DELETE FROM dbo.ezParentChildren WHERE ParentID = @UserID OR ChildID = @UserID
            DELETE FROM dbo.ezEmergencyContacts WHERE ezUserID = @ezUserID

            DELETE FROM dbo.ezDriveThrus_Customers WHERE ezUserID = @ezUserID   

            DELETE FROM dbo.ezRelationshipsP2P WHERE RelFromID = @UserID OR RelToID = @UserID

            DELETE FROM [dbo].[ezPL8Meaning] WHERE ezUserID = @ezUserID

            DELETE FROM dbo.ezUsers WHERE UserID = @UserID 

            DELETE FROM dbo.aspnet_PersonalizationPerUser WHERE UserID = @UserID
            DELETE FROM dbo.aspnet_Profile WHERE UserID = @UserID
            DELETE FROM dbo.aspnet_UsersInRoles WHERE UserID = @UserID

            DELETE FROM dbo.aspnet_Membership WHERE UserID = @UserID    

            DELETE FROM dbo.aspnet_Users WHERE UserID = @UserID     

    COMMIT TRANSACTION;  
END TRY

BEGIN CATCH
    SELECT ERROR_NUMBER(), ERROR_MESSAGE();
    IF @@TRANCOUNT > 0  
        ROLLBACK TRANSACTION;

    RAISERROR('Error', 16, 1)
    RETURN 0
END CATCH

END 






GO