Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reverting migration that adds column with default value throws #1345

Closed
mikary opened this issue Jan 2, 2015 · 3 comments
Closed

Reverting migration that adds column with default value throws #1345

mikary opened this issue Jan 2, 2015 · 3 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@mikary
Copy link
Contributor

mikary commented Jan 2, 2015

When attempting to revert a migration that adds an int column with a default value, migrations throws a SqlException.

Migration code:

    public partial class AddValue : Migration
    {
        public override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn("MyData", "Value", c => c.Int(nullable: false, defaultValue: 2));
        }

        public override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn("MyData", "Value");
        }
    }

Error:

> k ef migration apply 0
Using context 'MyContext'.
Reverting migration '201501021757503_AddValue'.
System.Data.SqlClient.SqlException (0x80131904): The object 'DF__MyData__Value__25869641' is dependent on column 'Value'.
ALTER TABLE DROP COLUMN Value failed because one or more objects access this column.

Script:

> k ef migration script AddValue InitialCreate
Using context 'MyContext'.
ALTER TABLE [MyData] DROP COLUMN [Value]
DELETE FROM [__MigrationHistory] WHERE [MigrationId] = '201501021757503_AddValue' AND [ContextKey] = 'MigrationDownRepro.MyContext'
@mikary
Copy link
Contributor Author

mikary commented Jan 2, 2015

Added reproduction here:
https://github.com/mikary/EF-Issue-1345

@natemcmaster
Copy link
Contributor

I found a similar bug. Dropping a nvarchar column with a default constraint also fails. I assume it is related to this issue.

@bricelam
Copy link
Contributor

Yep, SQL Server is silly and requires you to manually drop DEFAULT "constraints". I interpret this issue to mean handling it in general.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

5 participants