efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Migrations: Convert enum values to underlying type

Open delautour opened this issue 10 years ago • 8 comments

When creating a migration to add an enum column, the namespace of the enum is not included in the resulting migration.

EG:

  migrationBuilder.AddColumn<int>(
                name: "Status",
                table: "CommandRecord",
                nullable: false,
                defaultValue: CommandStatus.Pending);

This results in DNX,Version=v4.5.1 error CS0103: The name 'CommandStatus' does not exist in the current context when running dnx ef database update

delautour avatar Dec 13 '15 10:12 delautour

This is a bug. We shouldn't be referencing the enum value at all here--it may change over time. Instead we should use the int literal.

bricelam avatar Dec 14 '15 17:12 bricelam

@bricelam Actually it has to be a char, since an enum could have a char value. Ex.:

    public enum MyEnum
    {
        MyEnumItem = 'A'
    }

alexmurari avatar Dec 14 '15 19:12 alexmurari

To get technical, it will be the underlying type of the enum which, in .NET can be byte, sbyte, short, ushort, int, uint, long, or ulong. In your example, the character literal specified is implicitly converted to int--the underlying type of MyEnum--during compilation.

bricelam avatar Dec 14 '15 19:12 bricelam

Marking for re-triage. #4353 perpetuates this issue into the model snapshot.

bricelam avatar Jan 21 '16 20:01 bricelam

@smitpatel we had another issue where we decided to just convert default values to the underlying type (rather than storing as the enum value)... so that work should resolve this one too.

rowanmiller avatar Feb 01 '16 19:02 rowanmiller

The other issue (#4286) was not allowing user to set default value for enums. While fixing it, we removed serialization introduced (which was causing the types to be from a fixed list) and converted it to enum value. Since Migrations also have same issue that user-defined enum is used instead of underlying value (this issue), we decided to fix it in model snapshot together.

smitpatel avatar Feb 01 '16 19:02 smitpatel

So does that mean this one was fixed as part of the other change?

rowanmiller avatar Feb 01 '16 21:02 rowanmiller

The other one is partially fixed. It allows users to set default values now but it introduce this issue in model snapshot. Since migrations is already facing this issue, model snapshot & migrations can both be fixed together.

smitpatel avatar Feb 01 '16 21:02 smitpatel