Search code examples
c#entity-frameworkasp.net-mvc-4enumsfluent

How to mapped enum type to tinyint


I'm just wondering. Is it possible in fluent api or anything that an Enum type would be mapped as tinyint?

Say for example,

public enum PaperType
{
  Rough=1,
  Smooth=2,
}

public class TestPaper
{
   public PaperType PaperType { get; set; }
}

And if run migration correctly, it will mapped to an int which is not I don't want. I want to assigned tinyint so that it would not take much as space. int 4bytes while tinyint 1byte Any thoughts? Thanks!


Solution

  • You can do:

    public enum PaperType : byte
    {
      Rough=1,
      Smooth=2,
    }
    

    But don't do that, that is premature optimization. Only come to optimization like these if there is a problem in your application performance. (default under laying type of enum is int so I am not sure if having a byte as enum type would save you anything, it will be treated as int. It might be useful if you are planning to use an array of enum, in that case you will get byte[])