efcore: [DatabaseGenerated(DatabaseGeneratedOption.Identity)] incorrectly configures the column as SqlServer identity
Steps 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:
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 16 (10 by maintainers)
Commits related to this issue
- Make DatabaseGeneratedOption.Identity not throw for non-integer properties on SQL Server Fixes #7010 — committed to dotnet/efcore by AndriySvyryd 8 years ago
- Make DatabaseGeneratedOption.Identity only set ValueGenerated to OnAdd Don't set RequiresValueGenerated when configuring Identity on a property Add a convention to set RequiresValueGenerated when usin... — committed to dotnet/efcore by AndriySvyryd 8 years ago
- Make DatabaseGeneratedOption.Identity only set ValueGenerated to OnAdd Don't set RequiresValueGenerated when configuring Identity on a property Add a convention to set RequiresValueGenerated when usin... — committed to dotnet/efcore by AndriySvyryd 8 years ago
- Make DatabaseGeneratedOption.Identity only set ValueGenerated to OnAdd Don't set RequiresValueGenerated when configuring Identity on a property Add a convention to set RequiresValueGenerated when usin... — committed to dotnet/efcore by AndriySvyryd 8 years ago
- Make ValueGenerated.Never really mean that the value will not be generated neither on the client nor on the store Make the temporality of the client generated values depend on whether a value will be ... — committed to dotnet/efcore by AndriySvyryd 8 years ago
- Make ValueGenerated.Never really mean that the value will not be generated neither on the client nor on the store Make the temporality of the client generated values depend on whether a value will be ... — committed to dotnet/efcore by AndriySvyryd 8 years ago
- Make ValueGenerated.Never really mean that the value will not be generated neither on the client nor on the store Make the temporality of the client generated values depend on whether a value will be ... — committed to dotnet/efcore by AndriySvyryd 8 years ago
- Make ValueGenerated only apply to store generated values. Make the temporality of the client generated values depend on whether a value will be generated by the store. Make DatabaseGeneratedOption.Ide... — committed to dotnet/efcore by AndriySvyryd 8 years ago
- Make ValueGenerated only apply to store generated values. Make the temporality of the client generated values depend on whether a value will be generated by the store. Make DatabaseGeneratedOption.Ide... — committed to dotnet/efcore by AndriySvyryd 8 years ago
- Make ValueGenerated only apply to store generated values. Make the temporality of the client generated values depend on whether a value will be generated by the store. Make DatabaseGeneratedOption.Ide... — committed to dotnet/efcore by AndriySvyryd 8 years ago
- Make Property.RequiresValueGenerator to be a readonly extension method, this disables client-side value generation for non-key properties that don't have a custom value generator. Make DatabaseGenerat... — committed to dotnet/efcore by AndriySvyryd 8 years ago
- Make Property.RequiresValueGenerator to be a readonly extension method, this disables client-side value generation for non-key properties that don't have a custom value generator. Make DatabaseGenerat... — committed to dotnet/efcore by AndriySvyryd 8 years ago
- Make DatabaseGeneratedOption.Identity attribute just set ValueGenerated to OnAdd on SQL Server Part of #7010 — committed to dotnet/efcore by AndriySvyryd 8 years ago
- Make DatabaseGeneratedOption.Identity attribute just set ValueGenerated to OnAdd on SQL Server Part of #7010 — committed to dotnet/efcore by AndriySvyryd 8 years ago
- Make DatabaseGeneratedOption.Identity attribute just set ValueGenerated to OnAdd on SQL Server Part of #7010 — committed to dotnet/efcore by AndriySvyryd 8 years ago
@rodchenkov Soon™️. Most likely in this month.
This has been fixed and should be soon available on the ci-dev feed