错误“级联删除”

时间:2013-07-21 19:59:48

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

我有问题。我需要在CONSTRAINT [FK_SumaRoute_Suma]中添加“cascade delete on”:

CREATE TABLE [dbo].[SumaRoute] (
    [Id]      INT IDENTITY (1, 1) NOT NULL,
    [SumaId]  INT NOT NULL,
    [RouteId] INT NOT NULL,
    CONSTRAINT [PK_SumaRoute] PRIMARY KEY CLUSTERED ([Id] ASC),
    CONSTRAINT [FK_SumaRoute_Suma] FOREIGN KEY ([SumaId]) REFERENCES [dbo].[Suma] ([Id])  ***** HERE !!! *****,
    CONSTRAINT [FK_SumaRoute_Route] FOREIGN KEY ([RouteId]) REFERENCES [dbo].[Route] ([Id]) ON DELETE CASCADE
);

但是SQL Server返回错误:

  

创建FK_Part_Suma1 ...   (104,1):SQL72014:.Net SqlClient数据提供者:
  消息1785,级别16,状态0,行1   在表'Part'上引入FOREIGN KEY约束'FK_Part_Suma1'可能会导致循环或多个级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束   (104,1):SQL72014:.Net SqlClient数据提供者:
  Msg 1750,Level 16,State 0,Line 1
  无法创建约束或索引。请参阅先前的错误   批处理执行时发生错误。

我的桌子:

CREATE TABLE [dbo].[Suma] (
    [Id]       INT           IDENTITY (1, 1) NOT NULL,
    [SumaName] NVARCHAR (50) NOT NULL,
    CONSTRAINT [PK_Suma_1] PRIMARY KEY CLUSTERED ([Id] ASC)
);
CREATE TABLE [dbo].[Route] (
    [Id]        INT           IDENTITY (1, 1) NOT NULL,
    [RouteName] NVARCHAR (50) NOT NULL,
    CONSTRAINT [PK_Route] PRIMARY KEY CLUSTERED ([Id] ASC)
);

编辑:下表

CREATE TABLE [dbo].[Part] (
    [Id]       INT           IDENTITY (1, 1) NOT NULL,
    [PartName] NVARCHAR (50) NOT NULL,
    [SumaId]   INT           NOT NULL,
    [Place]    NVARCHAR (50) NULL,
    [Time]     TIME (7)      NULL,
    CONSTRAINT [PK_Part] PRIMARY KEY CLUSTERED ([Id] ASC),
    CONSTRAINT [FK_Part_Suma1] FOREIGN KEY ([SumaId]) REFERENCES [dbo].[Suma] ([Id])
);
CREATE TABLE [dbo].[Order] (
    [Id]             INT      IDENTITY (1, 1) NOT NULL,
    [PartId]         INT      NOT NULL,
    [RouteId]        INT      NULL,
    [SumaId]         INT      NOT NULL,
    [DriverId]       INT      NULL,
    [Express]        BIT      CONSTRAINT [DF_Order_Express] DEFAULT ((0)) NOT NULL,
    [Status]         INT      CONSTRAINT [DF_Order_Status] DEFAULT ((0)) NOT NULL,
    [TimeLeft]       DATETIME NOT NULL,
    [StartTransport] DATETIME NULL,
    [Created]        DATETIME NULL,
    CONSTRAINT [PK_Order] PRIMARY KEY CLUSTERED ([Id] ASC),
    CONSTRAINT [FK_Order_Part] FOREIGN KEY ([PartId]) REFERENCES [dbo].[Part] ([Id]) ON DELETE CASCADE,
    CONSTRAINT [FK_Order_Route] FOREIGN KEY ([RouteId]) REFERENCES [dbo].[Route] ([Id]) ON DELETE CASCADE,
    CONSTRAINT [FK_Order_Suma] FOREIGN KEY ([SumaId]) REFERENCES [dbo].[Suma] ([Id]) ON DELETE CASCADE,
    CONSTRAINT [FK_Order_User] FOREIGN KEY ([DriverId]) REFERENCES [dbo].[User] ([Id]) ON DELETE CASCADE
);
CREATE TABLE [dbo].[User] (
    [Id]            INT            IDENTITY (1, 1) NOT NULL,
    [Pin]           NCHAR (4)      NOT NULL,
    [SumaIdSigned]  INT            NULL,
    [RouteIdSigned] INT            NULL,
    [Name]          NVARCHAR (50)  NOT NULL,
    [Surname]       NVARCHAR (50)  NOT NULL,
    [Type]          BIT            NOT NULL,
    [TokenGCM]      NVARCHAR (400) NULL,
    [TokenAccess]   NVARCHAR (50)  NULL,
    [SignedTime]    DATETIME       NULL,
    CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED ([Id] ASC),
    CONSTRAINT [FK_User_Route] FOREIGN KEY ([RouteIdSigned]) REFERENCES [dbo].[Route] ([Id]),
    CONSTRAINT [FK_User_Suma] FOREIGN KEY ([SumaIdSigned]) REFERENCES [dbo].[Suma] ([Id])
);

我可以删除Suma,Route和User。如果我删除Suma行(id = 5)并且任何Order包含SumaId(5),那么我需要删除sumaId(5)的所有订单。

我做了什么? 对不起我的英文:)

0 个答案:

没有答案