Pergunta

Hi I'm new in POCO and I'm wondering if it's possible map a table as a enum. Look my simple diagram:

http://www.jonataspiazzi.xpg.com.br/outpost/diagram02.png

The table UserType represents a list of types, very much like a enum type in C# code. So I want to generate a class that has a property of type enum.

Something like this:

[Table("User")]
public class User
{
    public int UserId { get; set; }

    public string Name { get; set; }

    [ForeignKey("???")]
    public UserType Type { get; set; }
}

[Table("UserType")]
public enum UserType
{
    [Description("Administrator")]
    Administrator = 1,

    [Description("Operator")]
    Operator = 2,

    [Description("Consultor")]
    Consultor = 3
}

Is possible map the tables with some code like this?
*ps: this code is incorrect, it's only a way to try explan my question.

Foi útil?

Solução

No, its not possible. Entity should be reference type (enum is value type) and it should have id property. Enum is just set of named constants. There is no id property.

Workaround - you can save integer value in table User which can be mapped to your enum:

public class User
{
    public int UserId { get; set; }
    public string Name { get; set; }
    public UserType Type { get; set; }
}

public enum UserType
{
    Administrator = 1,
    Operator = 2,
    Consultor = 3
}

If you are using EF 4 then you should create integer property in User class for mapping to database column, and enum property which is not mapped but calculated via integer property:

[Column("UserType")]
public int UserTypeValue { get; set; }

[NotMapped]
public UserType Type
{
  get { return (UserType) UserTypeValue;
  set { UserTypeValue = (int)value;
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top