【问题标题】:How to mapped enum type to tinyint如何将枚举类型映射到 tinyint
【发布时间】:2014-08-29 16:01:51
【问题描述】:

我只是想知道。是否有可能在流利的 api 或 Enum 类型将映射为 tinyint 的任何东西?

比如说,

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

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

如果正确运行迁移,它将映射到我不想要的int。我想分配tinyint,这样它就不会占用太多空间。 int 4bytestinyint 1byte 有什么想法吗?谢谢!

【问题讨论】:

    标签: c# entity-framework asp.net-mvc-4 enums fluent


    【解决方案1】:

    你可以这样做:

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

    但不要这样做,那是过早的优化。只有在您的应用程序性能出现问题时才进行此类优化。 enum 的默认铺设类型为int,所以我不确定将byte 作为枚举类型是否会为您节省任何东西,它将被视为int。如果您使用它可能会很有用计划使用enum 的数组,在这种情况下你会得到byte[])

    【讨论】:

    • EF Core 1.1 的有效解决方案
    【解决方案2】:

    我不确定你要求的是什么:

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

    【讨论】:

      猜你喜欢
      • 2018-08-01
      • 2019-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多