VansFannel
VansFannel

Reputation: 45921

Using an enum in a POCO class

I'm developing an Entity Framework 6.1.3 library with .NET Framework 4.0 and C#.

I have a question about if it is possible to use an enum in a POCO class.

I have this class and enum:

public class MyClass
{
    public byte MyClassId { get; set; }
    public string Name { get; set; }
    public byte CodeType { get; set; }
    public byte HelperCodeType { get; set; }

    public virtual ICollection<Code> Codes { get; set; }
    public virtual ICollection<HelperCode> HelperCodes { get; set; }
}

public enum CodeTypes : byte
{
    Generated = 0,
    PrePrinted = 1,
    PrePrintedAndPreLoaded = 2,
    PreLoaded = 3,
    NotUsed = 4
}

I'm wondering if I can change MyClass fields CodeType and HelperCodeType with the enum type instead of byte:

public class MyClass
{
    public byte MyClassId { get; set; }
    public string Name { get; set; }
    public CodeTypes CodeType { get; set; }
    public CodeTypes HelperCodeType { get; set; }

    public virtual ICollection<Code> Codes { get; set; }
    public virtual ICollection<HelperCode> HelperCodes { get; set; }
}

Is it a good idea to use enums instead of byte in a POCO class?

Upvotes: 2

Views: 1126

Answers (1)

Georg Patscheider
Georg Patscheider

Reputation: 9463

Entity Framework supports enums since some time (since version 5 if I remember correctly). In the database, it will use the numeric type of the enum as column type, in your case it will add byte columns.

I think enums are a good idea if some business logic depends on the field.

Upvotes: 4

Related Questions