迁移未能降级

时间:2014-10-14 12:09:00

标签: entity-framework ef-migrations

使用Add-migration,我得到以下代码优先迁移:

public partial class MyMigration : DbMigration
{
    public override void Up()
    {
        DropForeignKey("dbo.Attachment", "EmployeePresentationID", "dbo.L_EmployeePresentation");
        DropForeignKey("dbo.Attachment", "TopicID", "dbo.Topic");
        DropForeignKey("dbo.Attachment", "UploaderID", "dbo.User");
    }

    public override void Down()
    {
        AddForeignKey("dbo.Attachment", "UploaderID", "dbo.User", "ID", cascadeDelete: true);
        AddForeignKey("dbo.Attachment", "TopicID", "dbo.Topic", "ID");
        AddForeignKey("dbo.Attachment", "EmployeePresentationID", "dbo.L_EmployeePresentation", "ID");
    }
}

代码缩短了,但没有修改。虽然向上迁移效果很好,但向下迁移失败了:

PM> Update-Database -verbose
Using StartUp project 'My_Project'.
Using NuGet project 'My_Project'.
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Target database is: 'My-Database' (DataSource: (LocalDb)\v11.0, Provider: System.Data.SqlClient, Origin: Configuration).
Applying explicit migrations: [201410131339480_MyMigration].
Applying explicit migration: 201410131339480_MyMigration.

IF object_id(N'[dbo].[FK_dbo.AttachedFile_dbo.L_EmpPr_EmpPrID]', N'F') IS NOT NULL
    ALTER TABLE [dbo].[AttachedFile] DROP CONSTRAINT [FK_dbo.AttachedFile_dbo.L_EmpPr_EmpPrID]
IF object_id(N'[dbo].[FK_dbo.AttachedFile_dbo.Topic_TopicID]', N'F') IS NOT NULL
    ALTER TABLE [dbo].[AttachedFile] DROP CONSTRAINT [FK_dbo.AttachedFile_dbo.Topic_TopicID]
IF object_id(N'[dbo].[FK_dbo.AttachedFile_dbo.User_UploaderID]', N'F') IS NOT NULL
    ALTER TABLE [dbo].[AttachedFile] DROP CONSTRAINT [FK_dbo.AttachedFile_dbo.User_UploaderID]

INSERT [dbo].[__MigrationHistory]([MigrationId], [ContextKey], [Model], [ProductVersion])
VALUES (N'201410131339480_MyMigration', N'My_Project.DataLayer.DataContext',  

/// LOOOOOOOONG hex-stuff

Running Seed method.


PM> Update-Database -Target tags_added -verbose

Using StartUp project 'My_Project'.
Using NuGet project 'My_Project'.
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Target database is: 'My-Database' (DataSource: (LocalDb)\v11.0, Provider: System.Data.SqlClient, Origin: Configuration).
Reverting migrations: [201410131339480_MyMigration].
Reverting explicit migration: 201410131339480_MyMigration.
ALTER TABLE [dbo].[AttachedFile] ADD CONSTRAINT [FK_dbo.AttachedFile_dbo.User_UploaderID] FOREIGN KEY ([UploaderID]) REFERENCES [dbo].[User] ([ID]) ON DELETE CASCADE
System.Data.SqlClient.SqlException (0x80131904): Introducing FOREIGN KEY constraint 'FK_dbo.AttachedFile_dbo.User_UploaderID' on table 'AttachedFile' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint. See previous errors.
  ....
Introducing FOREIGN KEY constraint 'FK_dbo.AttachedFile_dbo.User_UploaderID' on table 'AttachedFile' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint. See previous errors.
PM> 

错误令我感到困惑,因为数据库目前有外键。如果这是不可能的,我不应该有当前的数据库,对吗?

这是表结构:

/****** Object:  Table [dbo].[AttachedFile]    Script Date: 10/14/2014 14:05:55 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[AttachedFile](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [TopicID] [int] NULL,
    [Deleted] [datetime] NULL,
    [DisplayName] [nvarchar](max) NOT NULL,
    [SafeName] [nvarchar](max) NOT NULL,
    [Extension] [nvarchar](max) NOT NULL,
    [Created] [datetime] NOT NULL,
    [FileSize] [int] NOT NULL,
    [UploaderID] [int] NOT NULL,
    [EmployeePresentationID] [int] NULL,
 CONSTRAINT [PK_dbo.AttachedFile] PRIMARY KEY CLUSTERED 
(
    [ID] 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].[AttachedFile]  WITH CHECK ADD  CONSTRAINT [FK_dbo.AttachedFile_dbo.L_EmployeePresentation_EmployeePresentation_ID] FOREIGN KEY([EmployeePresentationID])
REFERENCES [dbo].[L_EmployeePresentation] ([ID])
GO

ALTER TABLE [dbo].[AttachedFile] CHECK CONSTRAINT [FK_dbo.AttachedFile_dbo.L_EmployeePresentation_EmployeePresentation_ID]
GO

ALTER TABLE [dbo].[AttachedFile]  WITH CHECK ADD  CONSTRAINT [FK_dbo.AttachedFile_dbo.Topic_TopicID] FOREIGN KEY([TopicID])
REFERENCES [dbo].[Topic] ([ID])
GO

ALTER TABLE [dbo].[AttachedFile] CHECK CONSTRAINT [FK_dbo.AttachedFile_dbo.Topic_TopicID]
GO

ALTER TABLE [dbo].[AttachedFile]  WITH CHECK ADD  CONSTRAINT [FK_dbo.AttachedFile_dbo.User_Uploader_ID] FOREIGN KEY([UploaderID])
REFERENCES [dbo].[User] ([ID])
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[AttachedFile] CHECK CONSTRAINT [FK_dbo.AttachedFile_dbo.User_Uploader_ID]
GO


/****** Object:  Table [dbo].[User]    Script Date: 10/14/2014 14:06:00 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[User](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Guid] [uniqueidentifier] NOT NULL,
    [ShortName] [nvarchar](max) NOT NULL,
    [LongName] [nvarchar](max) NULL,
    [EmailAddress] [nvarchar](max) NULL,
    [IsActive] [bit] NOT NULL,
    [ActiveSession_ID] [int] NULL,
    [Settings_ColorScheme] [int] NOT NULL,
    [SessionReport_ID] [int] NULL,
    [Settings_ReportOccasions] [int] NOT NULL,
 CONSTRAINT [PK_dbo.User] PRIMARY KEY CLUSTERED 
(
    [ID] 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].[User]  WITH CHECK ADD  CONSTRAINT [FK_dbo.User_dbo.ActiveSession_ActiveSession_ID] FOREIGN KEY([ActiveSession_ID])
REFERENCES [dbo].[ActiveSession] ([ID])
GO

ALTER TABLE [dbo].[User] CHECK CONSTRAINT [FK_dbo.User_dbo.ActiveSession_ActiveSession_ID]
GO

ALTER TABLE [dbo].[User]  WITH CHECK ADD  CONSTRAINT [FK_dbo.User_dbo.SessionReport_SessionReport_ID] FOREIGN KEY([SessionReport_ID])
REFERENCES [dbo].[SessionReport] ([ID])
GO

ALTER TABLE [dbo].[User] CHECK CONSTRAINT [FK_dbo.User_dbo.SessionReport_SessionReport_ID]
GO

ALTER TABLE [dbo].[User] ADD  DEFAULT ((0)) FOR [Settings_ColorScheme]
GO

ALTER TABLE [dbo].[User] ADD  DEFAULT ((0)) FOR [Settings_ReportOccasions]
GO

1 个答案:

答案 0 :(得分:0)

  

如果依赖实体上的外键不可为空,则代码   首先在关系上设置级联删除。

参考: EF Cascade on delete

因此,您的向下迁移是在上次迁移之前添加了一个不存在的级联。使用FluentAPI方法.WillCascadeOnDelete(false)指定外键不应在删除时级联。