0

I am using EF4.3.1 and MVC4 code first. My database is being created except for one column and no error is given. Why would this column not be created? Should I be using the EnumDataType attribute (it doesn't seem to do anything)?

public class Setting
{
    public Guid SettingId { get; set; }
    public string Key { get; set; }
    public string Value { get; set; }

    [Column("DataType", TypeName = "varchar")]
    public SettingDataType DataType { get; set; }
    }


    public enum SettingDataType
    {
        [Description("String")]
        text,
        [Description("Integer")]
        integer,
        [Description("Boolean")]
        boolean
    }

2 Answers 2

2

Enums are still not supported in EF 4.3. It does not make a difference whether you specify another DataType in the attribute.

Enums are simply completely ignored by EF when analyzing the class and generating the tables.

Enum support is promised for EF 5.0 though ... finally!

For now use an int Property instead and add another Property that casts the int to the enum value on the fly. Also just to be sure nothing breaks with the next EF version add the Ignore keyword.

public class Setting
{
    public Guid SettingId { get; set; }
    public string Key { get; set; }
    public string Value { get; set; }

    [Column("DataType", TypeName = "varchar")]
    public int DataTypeInt { get; set; }

    [Ignore]
    public SettingDataType DataType
    { 
        get { return (SettingDataType)this.DataTypeInt; }
        set { return this.DataTypeInt = (int)value; }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

Enums are not supported in EF until version 5.0. So, you are probably just hitting this limit.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.