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

[RC1] Invalid migrations snapshot generated for key Fluent APIs #26045

Closed
gabynevada opened this issue Sep 15, 2021 · 3 comments · Fixed by #26058
Closed

[RC1] Invalid migrations snapshot generated for key Fluent APIs #26045

gabynevada opened this issue Sep 15, 2021 · 3 comments · Fixed by #26058
Assignees
Labels
area-migrations closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression type-bug
Milestone

Comments

@gabynevada
Copy link

We were testing Preview 7 and it was working correctly but on RC1 we're having some typing issues with the migrations that are generated with EF Core 6.

We are using SQL Server 2019 and utilizing a different Cluster Index than our primary keys, but when ef core generates the ContextModelSnapshot it generates code that fails on build.

Include your code

Model class

public partial class Entity
{
    public Guid EntityId { get; set; }
    public int ClusterId { get; set; }
}

Model Configuration

  public class UserAddressConfiguration : IEntityTypeConfiguration<Entity>
  {
    public void Configure(EntityTypeBuilder<Entity> entity)
    {
      entity.ToTable("Entity", "entity");

      entity.HasKey(e => e.EntityId)
          .HasName("PKNC_Entity_EntityId")
          .IsClustered(false);

      entity.HasIndex(e => e.ClusterId)
          .HasDatabaseName("AKCI_Entity_ClusterId")
          .IsUnique()
          .IsClustered();
    }
  }

Partial generated code on ContextModelSnapshot

          b.HasKey("EntityId")
              .HasName("PKNC_Entity_EntityId");

          SqlServerKeyBuilderExtensions.IsClustered(b, false); // Error here

          b.HasIndex("ClusterId")
              .IsUnique()
              .HasDatabaseName("AKCI_Entity_ClusterId");

          SqlServerIndexBuilderExtensions.IsClustered(b.HasIndex("ClusterId"));

Error

  ContextModelSnapshot.cs(131, 63): [CS1503] Argument 1: cannot convert from 'Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder' to 'Microsoft.EntityFrameworkCore.Metadata.Builders.KeyBuilder'

Include provider and version information

EF Core version: 6.0.0-rc.1.21452.10
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6.0
Operating system: Mac OS Big Sur 11.5.2
IDE: Rider / VsCode

@roji
Copy link
Member

roji commented Sep 15, 2021

Confirmed, this seems to be a result of #23848. I'll look at this ASAP.

Minimal repro
await using var ctx = new BlogContext();
await ctx.Database.EnsureDeletedAsync();
await ctx.Database.EnsureCreatedAsync();

public class BlogContext : DbContext
{
    public DbSet<Entity> Entities { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder.UseSqlServer(@"Server=localhost;Database=test;User=SA;Password=Abcd5678;Connect Timeout=60;ConnectRetryCount=0");

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Entity>(entity => entity.HasKey(e => e.EntityId).IsClustered(false));
    }
}

public class Entity
{
    public Guid EntityId { get; set; }
    public int ClusterId { get; set; }
}

Creating a migration generates an invalid model snapshot.

@roji roji self-assigned this Sep 15, 2021
@roji roji changed the title Ef Core 6 RC1 - Typing error on migration with cluster index on different column [RC1] Invalid migrations snapshot generated for key Fluent APIs Sep 15, 2021
@chrissainty
Copy link

I started the upgrade of our platform to .NET 6 RC1 yesterday. We're also using a different clustered index to the primary keys and we're seeing exactly the same issue as the OP.

@roji
Copy link
Member

roji commented Sep 16, 2021

This will be fixed in RC2. As a workaround, manually edit your context snapshots and replace the non-compiling code:

SqlServerKeyBuilderExtensions.IsClustered(b, false);

With the following:

SqlServerKeyBuilderExtensions.IsClustered(b.HasKey("EntityId"), false);

(replacing EntityId with the name of your key column(s)).

roji added a commit that referenced this issue Sep 16, 2021
Also generate Fluent API for IsMemoryOptimized.

Fixes #26045
@roji roji added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Sep 16, 2021
roji added a commit that referenced this issue Sep 16, 2021
Also generate Fluent API for IsMemoryOptimized.

Fixes #26045
ajcvickers pushed a commit that referenced this issue Sep 16, 2021
Also generate Fluent API for IsMemoryOptimized.

Fixes #26045
@ajcvickers ajcvickers added this to the 6.0.0-rc2 milestone Sep 17, 2021
@ajcvickers ajcvickers modified the milestones: 6.0.0-rc2, 6.0.0 Nov 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-migrations closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants