Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ModelBuilder and UseIdentityColumn fails to set identity property on column #33684

Closed
simonfox opened this issue May 7, 2024 · 1 comment · Fixed by #33719
Closed

ModelBuilder and UseIdentityColumn fails to set identity property on column #33684

simonfox opened this issue May 7, 2024 · 1 comment · Fixed by #33719
Labels
area-model-building closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@simonfox
Copy link

simonfox commented May 7, 2024

Microsoft.EntityFrameworkCore.SqlServer 8.0.4

I'm using ModelBuilder to build a model up front; the ModelBuilder api feels more intuitive than the OnModelCreating approach to me. For my tests I use EnsureCreated to initialize a new database off of the model. I'm facing an issue with properties configured using UseIdentityColumn() where the identity property isn't being set on the column in the new database.

If I switch to use the OnModelCreating approach to setup the model, the identity property is set as expected.

The following code demonstrates the approach that is affected by this issue.

public class TestModel
{
    public virtual int Id { get; set; }
    public virtual string Code { get; set; } = string.Empty;
}

public class TestModelConfiguration : IEntityTypeConfiguration<TestModel>
{
    public void Configure(EntityTypeBuilder<TestModel> builder)
    {
        builder.ToTable("Test");
        builder.HasKey(x => x.Id);
        builder
            .Property(x => x.Id)
            .UseIdentityColumn();
        builder
            .Property(x => x.Code)
            .IsRequired()
            .HasMaxLength(70);
    }
}

ModelBuilder modelBuilder = new(SqlServerConventionSetBuilder.Build());
modelBuilder.ApplyConfigurationsFromAssembly(typeof(TestModelConfiguration).Assembly);

DbContextOptionsBuilder dbContextOptions = new();
dbContextOptions.UseSqlServer(connectionString);
dbContextOptions.UseModel(modelBuilder.FinalizeModel());

using TestDbContext dbContext = new(_dbContextOptions.Options);
dbContext.Database.EnsureCreated();
@AndriySvyryd
Copy link
Member

There's a workaround and the fix is not risk-free, so this will be fixed for 9.0

@AndriySvyryd AndriySvyryd added type-bug closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. labels May 14, 2024
@AndriySvyryd AndriySvyryd added this to the 9.0.0 milestone May 14, 2024
@AndriySvyryd AndriySvyryd removed their assignment May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-model-building closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants