Bill Greer
Bill Greer

Reputation: 3156

Enum in an Entity Framework POCO

I have the following employee class and employee status enum:

public class Employee
{
    public int ID { get; set; }
    public int ClockNo { get; set; }

    public string FirstName { get; set; }
    public string MiddleInitial { get; set; }
    public string LastName { get; set; }
    public EmployeeStatus Status { get; set; }


}

public enum EmployeeStatus : int
{
    New = 1,
    Experienced = 2,
    Terminated = 3

}   

I only want the enum made available to the Employee class so I tried nesting it:

public class Employee
{
    public int ID { get; set; }
    public int ClockNo { get; set; }

    public string FirstName { get; set; }
    public string MiddleInitial { get; set; }
    public string LastName { get; set; }
    public EmployeeStatus Status { get; set; }

    enum EmployeeStatus : int
    {
        New = 1,
        Experienced = 2,
        Terminated = 3

    }         

}

I get a compile time error of EmployeeStatus could not be found. How do I approach this problem ? I want my employee status to be limited to a set of options that I hard code and that intellisense makes available to me.

enter image description here

Upvotes: 0

Views: 277

Answers (1)

Sam Axe
Sam Axe

Reputation: 33738

You can't nest the enum in the class unless you make the enum public. Making the enum private like you have, while surfacing it via a public property (public EmployeeStatus Status { get; set; }) will cause this error.

Either make the enum public, or make the property private.

Upvotes: 3

Related Questions