Using Byte as Primary Key Type

I use Entity Framework code first. I have a table that will not exceed 100 rows, and I would like to use the byte data type ( tinyint in SQL Server) as the primary key.

This is what I still have:

 [Key] public byte Id { get; set; } 

The problem is, when the Entity Framework creates the database, it does not set the identity specification property, which allows rows to be automatically incremented upon insertion.

If I change the data type to Int16 ( smallint in SQL Server), everything works fine.

Is there a way to tell the Entity Framework to set the auto increment property, or cannot the byte be used as the primary key with the first Entity Framework code?

+7
source share
1 answer

The byte type is supported as a key and as an identifier column. By default, you should not mark the primary key byte as a person. But you can overwrite this default value:

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

It is not possible to set the Identity parameter for int , a long and short (and maybe more types?), But for byte (= tinyint in SQL Server). I realized this through testing, but could not find it officially registered anywhere.

+13
source

All Articles