0

I have a table with one column char(2)

CREATE TABLE COMPANY (
    EMP_ID            INTEGER NOT NULL,
    EMP_TYPE          CHAR(2) NOT NULL,
    EMP_NM            VARCHAR(35) NOT NULL
);

Using Entity Framework, I want to map the entity with enumeration.

public class Company
{
    public Company ()
    {
        Id = null;
        Type = Enums.EnumType.TipoCompany.Matriz;
        Name = string.Empty;
    }

    public int? Id{ get; set; }
    public string Name{ get; set; }
    public Enums.EnumType.TipoCompany TipoCompany { get; set; }
}

public class CompanyMap : EntityTypeConfiguration<Company>
{
    public CompanyMap()
    {
        ToTable("COMPANY");

        HasKey(t => new { t.Id});

        Property(t => t.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None).HasColumnName("EMP_ID");

        Property(t => t.Name)
            .IsRequired()
            .HasMaxLength(35)
            .HasColumnType("Varchar")
            .HasColumnName("EMP_NM");

        Property(t => t.TypeCompany)
          .IsRequired()
          .HasColumnType("Varchar")
          .HasColumnName("EMP_TYPE");
    }
}

public class EnumTypeCompany
{
    public enum TypeCompany
    {
        [Description("Matriz")]
        Matriz = 01,
        [Description("Filial")]
        Filial = 02,
    }
}

I'm getting the error:

Contexto.Type[Nullable=False,DefaultValue=]' of member 'TypoCompany' in type 'Contexto.Company' is not compatible with 'FirebirdClient.varchar

How to convert varchar to enumeration???

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
  • 1
    "How to convert varchar to enumeration???" You can't. Your question is like how can I map `string` to `int`. – Ivan Stoev Dec 30 '15 at 17:20
  • sorry I asked wrong, It is because in Java I would create a conversion class that would take the results from the database and turn into enumeration – Paulo Henrique Dec 30 '15 at 17:28

2 Answers2

0

If you use int instead of varchar for EMP_TYPE field the problem should be solved. When you get data from database int fields will be automatically cast into your enum.

Update

public class Company
{
    public Company ()
    {
        Id = null;
        TipoCompanyString = ((int)Enums.EnumType.TipoCompany.Matriz).ToString();
        TipoCompany = Enums.EnumType.TipoCompany.Matriz;
        Name = string.Empty;
    }

    public int? Id { get; set; }
    public string Name { get; set; }
    public string TipoCompanyString { get; set; }
    public Enums.EnumType.TipoCompany TipoCompany { 
        get {
            var intValue = Convert.ToInt32(TipoCompanyString);
            return (Enums.EnumType.TipoCompany)intValue;
        }
        set {
            TipoCompanyString = ((int)value).ToString()
        }
    }
}

public class CompanyMap : EntityTypeConfiguration<Company>
{
    public CompanyMap()
    {
        ToTable("COMPANY");

        HasKey(t => new { t.Id});

        Property(t => t.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None).HasColumnName("EMP_ID");

        Property(t => t.Name)
            .IsRequired()
            .HasMaxLength(35)
            .HasColumnType("Varchar")
            .HasColumnName("EMP_NM");

        Property(t => t.TipoCompanyString)
            .IsRequired()
            .HasColumnType("Varchar")
            .HasColumnName("EMP_TYPE");
    }
}

public class EnumTypeCompany
{
    public enum TypeCompany
    {
        [Description("Matriz")]
        Matriz = 01,
        [Description("Filial")]
        Filial = 02,
    }
}
erikscandola
  • 2,854
  • 2
  • 17
  • 24
  • are you sure about that? – Enamul Hassan Dec 30 '15 at 19:33
  • @manetsus yup, it's true – Alexander Derck Dec 30 '15 at 19:35
  • It works, but I can not change my database – Paulo Henrique Dec 31 '15 at 10:37
  • Create a new string property and map it instead of `TypeCompany`. In `TypeCompany` get and set must cast the new string property to enum. `get { var intValue = Convert.ToInt32(newStringProperty); return (Enums.EnumType.TipoCompany)intValue; }`. This should be solved the issue. – erikscandola Dec 31 '15 at 10:45
  • I posted the complete code. Let me know if it works for you. – erikscandola Dec 31 '15 at 10:58
  • thank you, it's working now. Do you know another way to do? I will need to do something more generic in the future. – Paulo Henrique Dec 31 '15 at 11:43
  • You're welcome. More generic... For example? – erikscandola Dec 31 '15 at 11:48
  • Imagine where I have a property like this post http://stackoverflow.com/questions/19370104/convert-value-when-mapping and my entity has several bool properties and enum. My class will become very large. I am new to C #, I do not know if that would be a solution: http://romiller.com/2013/01/23/ef6-code-first-mapping-all-private-properties-using-custom-conventions/ – Paulo Henrique Dec 31 '15 at 12:28
  • You can try whit [C# Generics](https://msdn.microsoft.com/en-us/library/512aeb7t.aspx). You need to add a generic to your class and then use it in your property to do the cast. [This](http://stackoverflow.com/questions/271347/making-a-generic-property) is how to do. – erikscandola Jan 02 '16 at 10:37
0

In c# you can't have an enum:string, only int, byte etc. What you can do is make a "fake" enum:

public static class FakeEnum
{
  public static readonly OptionOne = "blabla";
  public static readonly OptionTwo = "random";
}

The string value would be the value of the EMP_TYPE field in your database. You can use this a bit the same way as if it were an enum of string, for example in checks:

Company company = db.Companies.First();
if(company.Type == FakeEnum.OptionOne) //company.Type is really "blabla" in db
  DoStuff();
Alexander Derck
  • 13,818
  • 5
  • 54
  • 76