none
EF 5.0 Not Generating DB columns for Enums in Model RRS feed

  • Question

  •  I am using VS 2012, EF 5.0. I attempted to use Code First Migrations to add a table to an existing database. However, the Migration did not create columns for the properties in my class that are typed as Enum.

    Here is the class for which I want to generate a table in the DB:

        [Table("ObservationTemplates",Schema="Templates")]
        public class ObservationTemplate
        //template that defines an individual observation
        {
    #region PERSISTED PROPERTIES
        #region properties required by client
            //ALL DATA TYPES
            [Key, Display(Name = "ID")]
            public int observationTemplateID { get; set; }  //key
            [Display(Name = "Prompt", Order = 0), Required, MaxLength(300)]
            public string prompt { get; set; }              //prompts the user for entry of the data
            //[Display(Name="Data Type",Order=1,Prompt="Data Type"),Required,StringLength(20)]
            public Enums.ObservationTemplate.eDataType dataType { get; set; }           //type of data collected by this template
            public Enums.ObservationTemplate.eControlType controlType { get; set; }     //type of control used by this template
            public Enums.ObservationTemplate.eDisplayNote displayNote { get; set; }     //get (display) a note when using this OT
            public string initialValue { get; set; }                                    //default value for new observation
            public string helpText { get; set; }
            public Enums.ObservationTemplate.eLayout layout { get; set; }              //horizontal or vertical
            //TEXT DATA TYPE
            public int minTextLength { get; set; }          //minimum length of text
            public int maxTextLength { get; set; }          //maximum length of text
            //NUMERIC DATA TYPE
            public int numDecimals { get; set; }                //number of decimals
            public decimal minNumValue { get; set; }            //for validation and slider control
            public decimal maxNumValue { get; set; }            //for validation and slider control
            public decimal numValueStep { get; set; }           //used for slider control
            public string unitOfMeasure { get; set; }
            public bool autoAdvance { get; set; }           //auto advance to next observation, after single selection
            public int columnCount { get; set; }            //number of columns for layout of choices
        #endregion properties required by client
        #region properties NOT required at client
            public bool isPublic { get; set; }              //is this template available to the public
            //public string name { get; set; }                //short name displayed in lists
            public int ownerID { get; set; }                //ID of the participant who is the owner of this template
            public string category { get; set; }            //category of observation template (from code tables)
            public string type { get; set; }                //within category
            public string varName { get; set; }             //unique reference name for the data gathered by this template
            public Enums.ObservationTemplate.eStatus status { get; set; }             //current status of the ObservationTemplate
        #endregion properties NOT required at client
        #region navigation properties
            //public virtual ICollection<Trigger_ObservationTemplate> triggers { get; set; }   //triggers associated with the ObservationTemplate
            public virtual ICollection<OTchoice> choices { get; set; }     //choices associated with the ObservationTemplate
        #endregion navigation properties
    #endregion PERSISTED PROPERTIES
    
    
            //   CONSTRUCTOR  //
            public ObservationTemplate()
            {
                //set defaults
                ownerID=0;
                isPublic=true;
                displayNote = Enums.ObservationTemplate.eDisplayNote.None;
                status = Enums.ObservationTemplate.eStatus.Draft;
            }
        }

    Here is the file generated by the Migration:

        public partial class add_OT_and_OTchoices : DbMigration
        {
            public override void Up()
            {
                CreateTable(
                    "Templates.ObservationTemplates",
                    c => new
                        {
                            observationTemplateID = c.Int(nullable: false, identity: true),
                            prompt = c.String(),
                            initialValue = c.String(),
                            helpText = c.String(),
                            minTextLength = c.Int(nullable: false),
                            maxTextLength = c.Int(nullable: false),
                            numDecimals = c.Int(nullable: false),
                            minNumValue = c.Decimal(nullable: false, precision: 18, scale: 2),
                            maxNumValue = c.Decimal(nullable: false, precision: 18, scale: 2),
                            numValueStep = c.Decimal(nullable: false, precision: 18, scale: 2),
                            unitOfMeasure = c.String(),
                            autoAdvance = c.Boolean(nullable: false),
                            columnCount = c.Int(nullable: false),
                            isPublic = c.Boolean(nullable: false),
                            ownerID = c.Int(nullable: false),
                            category = c.String(),
                            type = c.String(),
                            varName = c.String(),
                        })
                    .PrimaryKey(t => t.observationTemplateID);


    As you can see, class properties such as controlType, dataType, displayNote which have an Enum type are not created in the database.

    Any help or advice would be appreciated.

    Rob




    • Edited by RobinM2 Wednesday, November 6, 2013 2:02 AM
    Wednesday, November 6, 2013 1:53 AM

Answers

  • Let thses enum outside the class ObservationTemplate. Do not embed it in a class.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, November 7, 2013 10:07 AM
    Moderator

All replies

  • Hello,

    It is strange for that. Because I made a test with the same environment VS2012, .Net Framework 4.5 and Entity Framework 5, however, it worked fine.

    The new added table codes generated by the Migration:

    namespace Code_First.Migrations
    
    {
    
        using System;
    
        using System.Data.Entity.Migrations;
    
        
    
        public partial class ADDAntherTableWithEnumType : DbMigration
    
        {
    
            public override void Up()
    
            {
    
                CreateTable(
    
                    "dbo.DPTests",
    
                    c => new
    
                        {
    
                            DepartmentID = c.Int(nullable: false),
    
                            Budget = c.Decimal(nullable: false, precision: 18, scale: 2),
    
                            Name = c.Int(nullable: false),
    
                        })
    
                    .PrimaryKey(t => t.DepartmentID);
    
                
    
            }
    
            
    
            public override void Down()
    
            {
    
                DropTable("dbo.DPTests");
    
            }
    
        }
    
    }
    

    The name column is an enum type defined like below:

    namespace Code_First
    
    {
    
        public enum DepartmentNames
    
        {
    
            English,
    
            Math,
    
            Economics
    
        }
    
    }
    

    And the new entity class:

    namespace Code_First
    {
        public class DPTest
        {
            public int DepartmentID { get; set; }
            public decimal Budget { get; set; }
    
            public DepartmentNames Name { get; set; }
        }
    }

    Could you please share how you define the enum so that we could help you better?

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, November 6, 2013 6:43 AM
    Moderator
  • The enums are in their own class, within their own namespace. The enum definitions are as follows:

    namespace vnsAssessment.Templates.Enums
    {
        public class ObservationTemplate
        {
            public enum eLayout
                //possible layouts for OT
            {
                Vertical=1,
                Horizontal=2
            }
            public enum eDataType
                //supported data types
            {
                SelectOneFromList=1,
                SelectManyFromList=2,
                TrueFalse=3,
                YesNo=4,
                Numeric=5,
                Text=6,
                Date=7,
                Time=8,
                DateTime=9,
                Ratio=10,
                Invisible=11,
                Heading=12
            }
            public enum eControlType
                //supported control types ... specific control types supported for specific data types
            {
                //single choice selection
                SingleChoicePanels=1,
                MutExcCheckBoxes=2,
                RadioButtons=3,
                DropDownList=4,
                ListBox=5,
                //multiple choice selection
                MultiChoicePanels=6,
                ListBoxMultipleChoice=7,
                CheckBoxes=8,
                //true-false
                CheckBox=9,       //can also use single choice selection controls for true-false
                //text data
                TextBoxSingleLine=10,
                TextBoxMultiLine=11,
                //numeric data
                NumericTextBox=12,
                Slider=13,
                SliderWithTextBox=14,
                Spinner=15,
                //date / time data
                TimeSelector=16,
                DateTimePicker=17,
                DatePicker=18
            }
            public enum eDisplayNote
                //whether or not a note should be collected with observation
            {
                None=0,
                Optional=1,
                Mandatory=2
            }
            public enum eStatus
                //status of template ... can be used if Published or Locked
            {
                Draft=1,
                Published=2,
                Locked=3,
                Obsolete=4
            }
        }

    Also, it looks like EntityFramework has been upgraded within my project to 6.0.1 (rather than 5.0).

    Could the fact that the enums are not defined within the same namespace / class be part of the problem?

    Rob


    • Edited by RobinM2 Wednesday, November 6, 2013 3:56 PM
    Wednesday, November 6, 2013 3:53 PM
  • Let thses enum outside the class ObservationTemplate. Do not embed it in a class.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, November 7, 2013 10:07 AM
    Moderator