Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 717
Boy Pasmo Avatar asked Jan 10 '23 22:01

Boy Pasmo


2 Answers

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[])

like image 133
Habib Avatar answered Jan 21 '23 19:01

Habib


I'm not sure that's what you are asking for:

public enum PaperType : byte
{
  Rough=1,
  Smooth=2,
}
like image 39
Wagner DosAnjos Avatar answered Jan 21 '23 20:01

Wagner DosAnjos