[DatabaseGenerated(DatabaseGeneratedOption.Identity)] incorrectly configures the column as SqlServer identity
See original GitHub issueSteps to reproduce
I tried using the [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
attribute on a DateTime (which has a default value constraint on the column) as described in the Generated Properties documentation and I got an error as shown below:
Personally I think the documentation is wrong, as the [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
attribute is pretty specific. I successfully used the Fluent API .ValueGeneratedOnAdd() to tell EF Core there was a default value constraint.
The issue
- Create a table with a column ‘DateOfBirth’ with a default constraint on a
[datetime2]
column, e.g.
[DateOfBirth] [datetime2](7) NOT NULL DEFAULT ('2000-01-01T00:00:00.000'),
- Map that column to a property ‘DateOfBirth’
- Use the
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
attribute to tell EF Core that the column has a default constraint. - Create the DbContext and it fails with the following stack trace
Exception message: Identity value generation cannot be used for the property 'DateOfBirth' on entity type 'PersonWithAddUpdateAttibutes' because the property type is 'DateTime'. Identity value generation can only be used with signed integer properties.
Stack trace:
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.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider)
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.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
at Microsoft.EntityFrameworkCore.DbContext.get_StateManager()
at Microsoft.EntityFrameworkCore.DbContext.EntryWithoutDetectChanges[TEntity](TEntity entity)
at Microsoft.EntityFrameworkCore.DbContext.SetEntityState[TEntity](TEntity entity, EntityState entityState)
at test.UnitTests.TestPeopleContext.TestPeopleContextExistingDatabaseOk() in C:\Users\Jon\Documents\Visual Studio 2015\Projects\EfCoreInAction\test\UnitTests\TestPeopleContext.cs:line 111
Further technical details
EF Core version: “Microsoft.EntityFrameworkCore.SqlServer”: “1.1.0-preview1-final”, Operating system: Windows 10 Visual Studio version: VS2015 update 3
Other details about my project setup:
Issue Analytics
- State:
- Created 7 years ago
- Comments:16 (10 by maintainers)
Top Results From Across the Web
Entity Framework Core does not respect Identity columns
Entity Framework is not respecting my Identity columns. It insists on trying to insert a value into an Identity (auto-increment) column in my...
Read more >EF Core 6 Identity value generation problem
I am using EF Core 6 and SQL server express and trying to migrate my entities. I am getting this error. The properties...
Read more >EF Core 6 Identity value generation problem
I am using EF Core 6 and SQL server express and trying to migrate my entities. I am getting this error. The properties...
Read more >DatabaseGenerated Attribute in EF 7 & EF Core
As you know, EF creates an IDENTITY column in the database for all the id (key) properties of the entity, by default. So,...
Read more >Generated Values - EF Core
Database columns can have their values generated in various ways: ... key in SQL Server is automatically set up to be an IDENTITY...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@rodchenkov Soon™️. Most likely in this month.
This has been fixed and should be soon available on the ci-dev feed