Closed cjcfreitas closed 7 years ago
@cjcfreitas Could you explain a little about why you need this? Why is it that you need a tinyint column for this rather than something slightly larger? Are you sure that the number of rows won't exceed the value space?
Note for triage: The problem with allowing this is that there is a a high chance of overlap between temporary values and real values. Temporary values start at 255 and count down. Real values start at 1 and count up.
@ajcvickers One can always use a larger format. I use tinyint as PK in small master tables (e.g. document types, wharehouses, VAT taxes) when I am sure there will be only a few items. This will be FK in larger tables. I know there are different opinions about this, but I usually favor using the most restrictive data-type available whenever I am sure it wont overflow (Hard disk space is cheap but it's not the only issue).
We think we should make this a warning (with a default behavior of warn rather than throw). Any pushback @ajcvickers?
Sounds good.
Just wondering, but does EF Core hates tinyint? Tinyint is something that should be used if you know you never need more than 255. For example, in my table I use tinybit on EmployeeRoles, that means, is a standard employee, Team leader, and Manager, that is it, why would I use smallint or int in this case? EF Core should accept that DBs do use tinyint. :)
FWIW using auto-increment value generation for a type as small and constrained as tinyint sounds like asking for trouble... tinyint definitely makes sense when you have a closed set of known values which will never exceed 255, but if you're using value generation that already means you're not quite sure how many you're going to have...
@spyofborg in other words, just use tinyint and assign the key values yourself
Steps to reproduce
Trying to create a table where primary key is of type tinyint and is identity. Tried with anotations
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
and fluent APIUseSqlServerIdentityColumn()
The issue
I get this error:
Identity value generation cannot be used for the property 'Id' on entity type 'Cargo' because the property type is 'byte'. Identity value generation can only be used with signed integer properties.
Further technical details
SQL server does allow tinyint to be used with IDENTITY. As a workaround I used this:
[Key, Column(TypeName = "tinyint IDENTITY(1,1)")]
EF Core version: 1.1 Operating system: Windows 10 Visual Studio version: VS2015 Community
Best regards.