question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DbContext: Identity value generation cannot be used because the property type is byte

See original GitHub issue

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 API UseSqlServerIdentityColumn()

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.

Exception message:
Stack trace:
Project DFWeb (.NETCoreApp,Version=v1.1) was previously compiled. Skipping compilation.
dotnet : System.ArgumentException: 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.
At line:1 char:1
+ dotnet ef migrations add "Inicial"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (System.Argument...ger properties.:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 
   at Microsoft.EntityFrameworkCore.Metadata.SqlServerPropertyAnnotations.SetValueGenerationStrategy(Nullable`1 value)


   at Microsoft.EntityFrameworkCore.Metadata.Internal.SqlServerPropertyBuilderAnnotations.ValueGenerationStrategy(Nullable`1 value)


   at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.SqlServerValueGenerationStrategyConvention.Apply(InternalPropertyBuilder propertyBuilder, 
DatabaseGeneratedAttribute attribute, MemberInfo clrMember)


   at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.PropertyAttributeConvention`1.Apply(InternalPropertyBuilder propertyBuilder)


   at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.OnPropertyAdded(InternalPropertyBuilder propertyBuilder)


   at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityType.AddProperty(String name, Type propertyType, MemberInfo memberInfo, ConfigurationSource 
configurationSource, Boolean runConventions)


   at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityType.AddProperty(MemberInfo memberInfo, ConfigurationSource configurationSource, Boolean 
runConventions)


   at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.Property(Property existingProperty, String propertyName, Type propertyType, 
MemberInfo clrProperty, Nullable`1 configurationSource)


   at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.Property(String propertyName, Type propertyType, MemberInfo clrProperty, 
Nullable`1 configurationSource)


   at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.Property(MemberInfo clrProperty, ConfigurationSource configurationSource)


   at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.PropertyDiscoveryConvention.Apply(InternalEntityTypeBuilder entityTypeBuilder)


   at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.OnEntityTypeAdded(InternalEntityTypeBuilder entityTypeBuilder)


   at Microsoft.EntityFrameworkCore.Metadata.Internal.Model.AddEntityType(EntityType entityType, Boolean runConventions)


   at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalModelBuilder.Entity(Type type, ConfigurationSource configurationSource, Boolean runConventions)


   at Microsoft.EntityFrameworkCore.ModelBuilder.Entity(Type type)

   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.FindSets(ModelBuilder modelBuilder, DbContext context)


   at Microsoft.EntityFrameworkCore.Infrastructure.Internal.RelationalModelSource.FindSets(ModelBuilder modelBuilder, DbContext context)


   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator 
validator)


   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)


   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()


   at Microsoft.EntityFrameworkCore.Internal.LazyRef`1.get_Value()

   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)


   at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass16_0.<RealizeService>b__0(ServiceProvider provider)


   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider)


   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransient(TransientCallSite transientCallSite, ServiceProvider 
provider)


   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, 
ServiceProvider provider)


   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransient(TransientCallSite transientCallSite, ServiceProvider 
provider)


   at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass16_0.<RealizeService>b__0(ServiceProvider provider)

   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)


   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)

   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)


   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)


   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()


   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)


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.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ErikEJcommented, May 16, 2020

@spyofborg in other words, just use tinyint and assign the key values yourself

1reaction
cjcfreitascommented, Dec 2, 2016

@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).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Identity value generation can only be used with signed ...
The command fails with this error: Identity value generation cannot be used for the property 'QASourceID' on entity type 'C_QATrackSource' ...
Read more >
Generated Values - EF Core
How to configure value generation for properties when using Entity Framework Core.
Read more >
Value Conversions - EF Core
Value converters allow property values to be converted when reading from or writing to the database. This conversion can be from one value...
Read more >
Entity Framework Data Type Mapping - Documentation - Devart
PostgreSQL data types SSDL.NET boolean boolean System.Boolean smallint, int2 smallint System.Int16 integer, int4 int System.Int32
Read more >
What's New in EF Core 5 - Learn Entity Framework Core 7
EF Core uses the CLR default of type property type as a sentinel for this; if the value is not the CLR default,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found