none
enum <code first apoorach> RRS feed

  • Question

  • Hi,

    i am using Entity framework 5.0  (using ASP.NET MVC 4)How should i use enum through code first approach?

    i am using below mechanism for dealing enums.

     public enum Frequency
        {
            Daily,
            Monthly,
            TwoMonths,
            Quarterly,
            HalfYearly,
            Yearly
        }


     int frequency;
            public virtual Frequency FeeFrequency 
            {
                get { return (Frequency)frequency; }
                set { frequency = (int)value; }
            }

    But i want best recommended approach for dealing enums


    • Edited by chromeFor Monday, October 28, 2013 3:26 AM
    Monday, October 28, 2013 2:52 AM

Answers

  • Hello chromeFor,

    >>But I want best recommended approach for dealing enums

    Generally, I do not think there is a best way and there is a better way.

    For using enum in Entity Framework, we usually will write codes like below:

    public enum DepartmentNames { English, Math, Economics } public partial class Department {

    public int DepartmentID { get; set; } public DepartmentNames Name { get; set; } public decimal Budget { get; set; } }


    And we can call it like below:

    using (var context = new EnumTestContext())
    {
       context.Departments.Add(new Department { Name = DepartmentNames.English });
     
        context.SaveChanges();
     
        var department = (from d in context.Departments
                            where d.Name == DepartmentNames.English
                            select d).FirstOrDefault();
     
        Console.WriteLine(
            "DepartmentID: {0} Name: {1}",
            department.DepartmentID, 
            department.Name);
    }
    

    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.

    • Marked as answer by chromeFor Friday, November 1, 2013 4:16 PM
    Tuesday, October 29, 2013 5:50 AM
    Moderator
  • >>how in your approach?

    It would be string value.

    If is it that you want it to be integer or others?

    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.

    • Marked as answer by chromeFor Friday, November 1, 2013 4:16 PM
    Thursday, October 31, 2013 7:36 AM
    Moderator

All replies

  • Hello chromeFor,

    >>But I want best recommended approach for dealing enums

    Generally, I do not think there is a best way and there is a better way.

    For using enum in Entity Framework, we usually will write codes like below:

    public enum DepartmentNames { English, Math, Economics } public partial class Department {

    public int DepartmentID { get; set; } public DepartmentNames Name { get; set; } public decimal Budget { get; set; } }


    And we can call it like below:

    using (var context = new EnumTestContext())
    {
       context.Departments.Add(new Department { Name = DepartmentNames.English });
     
        context.SaveChanges();
     
        var department = (from d in context.Departments
                            where d.Name == DepartmentNames.English
                            select d).FirstOrDefault();
     
        Console.WriteLine(
            "DepartmentID: {0} Name: {1}",
            department.DepartmentID, 
            department.Name);
    }
    

    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.

    • Marked as answer by chromeFor Friday, November 1, 2013 4:16 PM
    Tuesday, October 29, 2013 5:50 AM
    Moderator
  • Hi

    What is Name?

    Sorry i think either i couldn't explain or you misunderstood.

    How EF preserve enums' value?

    in my above case, values would be stored an an interger in database.

    how in your approach?

    Wednesday, October 30, 2013 4:59 AM
  • >>how in your approach?

    It would be string value.

    If is it that you want it to be integer or others?

    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.

    • Marked as answer by chromeFor Friday, November 1, 2013 4:16 PM
    Thursday, October 31, 2013 7:36 AM
    Moderator