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.
[datetime2] column, e.g. [DateOfBirth] [datetime2](7) NOT NULL DEFAULT ('2000-01-01T00:00:00.000'),
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] attribute to tell EF Core that the column has a default constraint.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
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:
Which version includes this fix on the ci-dev builds?
@ninety7 This hasn't been fixed yet.
@AndriySvyryd putting in 1.1.1 as we think this may have been a breaking change in 1.1? Once all the code changes are done, can you give to me before closing so that I can update docs.
This has been fixed and should be soon available on the ci-dev feed
@rowanmiller For doc update: after #7204 only key properties will get a client-side value generated (unless a custom value generator is configured). This also means that UseSqlServerIdentityColumn and ForSqlServerUseSequenceHiLo will only work on key properties.
@AndriySvyryd
Can you please tell when fix for this will be in Nuget
(Identity value generation cannot be used for the property 'Id' on entity type 'ApplicationUser' because the property type is 'Guid'. Identity value generation can only be used with signed integer properties.)
Thank you!
This patch bug is approved. Please use the normal code review process w/ a PR and make sure the fix is in the correct branch, then close the bug and mark it as done.
Does this fix avaliable in 1.1 version? Or at any version that I can use with build and run xunit tests.
@rodchenkov The fix will be available in 1.1.1
@AndriySvyryd thank you!
Do you know when it will be avaliable?
@rodchenkov Soon™️. Most likely in this month.
Yep, what @AndriySvyryd said. Should be Feb.
@AndriySvyryd @Eilon Thank you!
Verified that this is fixed in the 1.1.1 candidate build.
✅ Verified
Hi, i tried doing this implementation of adding ValueGeneratedOnAdd() instead of DatabaseGenerated. but it doesnt work. My column is also indexed and unique, and i have another column that represents the primary key. I posted in SO - https://stackoverflow.com/questions/47152056/ef-core-unique-indexed-auto-generated-guid-column-that-is-not-the-primary-ke
Most helpful comment
@rodchenkov Soon™️. Most likely in this month.