Revert a migration w. default value crashes

I have a migration adding a column. It’s a not null column, so a default value is need. Per doc it should be added as an attribute:

public class Tag
{
        [Default(0)]
        public bool Deleted { get; set; }
}

It creates a constraint that adds the default value. It seems the name of it is auto generated, but it’s possible to name it as well, when the column is added:

ADD ExampleName varchar(1) NOT NULL CONSTRAINT DF_ExampleName DEFAULT('w');

which could be useful, because dropping it is necessary to drop the column:

alter table tag drop constraint DF__Tag__Deleted__7132C993
alter table tag drop column Deleted;

migrate.revert:last crashes because it forgets to remove the constraint/default before the column.

Default Value Constraints doesn’t work across most Databases, it basically only supported in SQL Server.

So you’ll need to opt-in to create a named constraint with WithConstraint, e.g:

public class Tag
{
    [Default(0, WithConstraint = "DF_Deleted")]
    public bool Deleted { get; set; }
}

This is available from the latest v8.2.3+ that’s now available in Pre Release packages.

1 Like

This works now. Confirmed.

1 Like