Using byte as the primary key datatype

后端 未结 1 1074
自闭症患者
自闭症患者 2020-12-11 17:02

I am using Entity Framework code-first. I have a table the will not exceed 100 rows and I would like to use the datatype byte (tinyint in SQL Serve

1条回答
  •  囚心锁ツ
    2020-12-11 17:37

    The byte type is supported as key and as an identity column. It is just not the default to mark the byte primary key as identity. But you can overwrite this default:

    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public byte Id { get; set; }
    

    Setting the Identity option explicitly is not necessary for an int, a long and a short (and perhaps more types?), but it is for a byte (= tinyint in SQL Server). I figured it out by testing but couldn't find it officially documented anywhere.

    0 讨论(0)
提交回复
热议问题