Migrations: Convert enum values to underlying type
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
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
Actually it has to be a char, since an enum could have a char value.
Ex.:
public enum MyEnum
{
MyEnumItem = 'A'
}
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.
Marking for re-triage. #4353 perpetuates this issue into the model snapshot.
@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.
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.
So does that mean this one was fixed as part of the other change?
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.