-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Issue still exists with 32-bit identity seed values in migrations #26062
Comments
Duplicate of #25589. Fixed for rc2. |
@ajcvickers We don't have any of those method calls in our migrations...
We do have
Is there a work around for this? |
/cc @roji |
This doesn't repro for me for a simple scenario:
I have the exact same line in the migration ( Can you help us out by providing more details on exactly what you're doing, ideally with some sort of repro? Attempted reproConsole programusing var ctx = new ClusterContext();
ctx.Database.Migrate();
public class ClusterContext : DbContext
{
public DbSet<Cluster> Blogs { get; set; }
static ILoggerFactory ContextLoggerFactory
=> LoggerFactory.Create(b => b.AddConsole().AddFilter("", LogLevel.Information));
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlServer(@"Server=localhost;Database=test;User=SA;Password=Abcd5678;Connect Timeout=60;ConnectRetryCount=0")
.EnableSensitiveDataLogging()
.UseLoggerFactory(ContextLoggerFactory);
}
public class Cluster
{
public int ClusterId { get; set; }
public string Name { get; set; }
}
#### Generated migration
Generated up migration:
```c#
migrationBuilder.CreateTable(
name: "Blogs",
columns: table => new
{
ClusterId = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Name = table.Column<string>(type: "nvarchar(max)", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Blogs", x => x.ClusterId);
}); |
@roji I've managed to repro this issue with the following.
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.Extensions.Logging;
namespace ConsoleApp2
{
internal class Program
{
static void Main(string[] args)
{
using var ctx = new ClusterContext();
ctx.Database.Migrate();
}
}
public class ClusterContext : DbContext
{
public DbSet<Cluster> Blogs { get; set; }
static ILoggerFactory ContextLoggerFactory
=> LoggerFactory.Create(b => b.AddConsole().AddFilter("", LogLevel.Information));
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfiguration(new ClusterEntityConfiguration());
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlServer(@"Server=localhost;Database=test;User=SA;Password=6U6UhlRj3wNvidt8O3AEDAdB;Connect Timeout=60;ConnectRetryCount=0")
.EnableSensitiveDataLogging()
.UseLoggerFactory(ContextLoggerFactory);
}
public class Cluster
{
public int ClusterId { get; set; }
public string Name { get; set; }
}
public class ClusterEntityConfiguration : IEntityTypeConfiguration<Cluster>
{
public void Configure(EntityTypeBuilder<Cluster> builder)
{
builder.HasKey(x => x.ClusterId).IsClustered(false);
builder.Property(x => x.ClusterId).UseIdentityColumn();
builder.HasIndex(x => x.ClusterId).IsUnique().IsClustered();
}
}
}
I believe the issue is in the .Designer file. When I generated it with .NET 6, this is what was produced: namespace ConsoleApp1.Migrations
{
[DbContext(typeof(ClusterContext))]
[Migration("20210916102727_Repro")]
partial class Repro
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "6.0.0-rc.1.21452.10")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); // Notice this line!!
modelBuilder.Entity("Cluster", b =>
{
b.Property<int>("ClusterId")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("ClusterId"), 1L, 1);
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("ClusterId");
SqlServerKeyBuilderExtensions.IsClustered(b.HasKey("ClusterId"), false);
b.HasIndex("ClusterId")
.IsUnique();
SqlServerIndexBuilderExtensions.IsClustered(b.HasIndex("ClusterId"));
b.ToTable("Blogs");
});
#pragma warning restore 612, 618
}
}
} Notice the presence of this line: SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); However, when generated with .NET 5 this is what the designer file looks like: namespace ConsoleApp2.Migrations
{
[DbContext(typeof(ClusterContext))]
[Migration("20210916103836_Repro")]
partial class Repro
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("Relational:MaxIdentifierLength", 128)
.HasAnnotation("ProductVersion", "5.0.10")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
modelBuilder.Entity("ConsoleApp2.Cluster", b =>
{
b.Property<int>("ClusterId")
.ValueGeneratedOnAdd()
.HasColumnType("int")
.HasAnnotation("SqlServer:IdentityIncrement", 1)
.HasAnnotation("SqlServer:IdentitySeed", 1)
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<string>("Name")
.HasColumnType("nvarchar(max)");
b.HasKey("ClusterId")
.IsClustered(false);
b.HasIndex("ClusterId")
.IsUnique()
.IsClustered();
b.ToTable("Blogs");
});
#pragma warning restore 612, 618
}
}
} Notice this line: The workaround seem to be to update the following which I now realise is what the suggested workaround was in the linked issue 🤦♂️ b.Property<int>("ClusterId")
.ValueGeneratedOnAdd()
.HasColumnType("int")
.HasAnnotation("SqlServer:IdentityIncrement", 1)
.HasAnnotation("SqlServer:IdentitySeed", 1)
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); to b.Property<int>("ClusterId")
.ValueGeneratedOnAdd()
.HasColumnType("int")
.HasAnnotation("SqlServer:IdentityIncrement", 1)
.HasAnnotation("SqlServer:IdentitySeed", 1L) // Added the "L" here
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); |
Thanks, this repros for me now, will investigate 👍 |
This does seem to be the same as #25589, but the fix there is in the snapshot model processor, which only seems to get invoked when scaffolding or removing a migration; when applying migrations we don't go through the processor. @AndriySvyryd I've submitted #26066 which adds the proper int->long conversions in the metadata API and the annotation code generator, plus a test - take a look. @chrissainty you can indeed work around this by editing your model snapshots and adding the L to make the value a long. |
I've been upgrading our solution from .NET 5 to .NET 6. After upgrading all package to the latest RC1 versions, our applications migrations no longer function. These migrations do work fine under .NET 5.
This is the exception being produced when running the application (migrations are applied during app startup during local development):
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 RC 1
The text was updated successfully, but these errors were encountered: