Pomelo.EntityFrameworkCore.MySql
Pomelo.EntityFrameworkCore.MySql copied to clipboard
AUTO_INCREMENT not added with custom column type
Steps to reproduce
class Test
{
public uint Id { get; set; }
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<DepthMaskContour>(entity =>
{
entity.HasKey(e => e.Id)
.HasName("PRIMARY");
entity.Property(e => e.Id).HasColumnType("int(11) unsigned");
});
}
Create a class with a primary key and add the shown column type.
The issue
The class should add .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn) to the migration file and finally the auto_increment extra property to the mysql database column. The ValueGeneratedOnAdd() is added correctly to the model snapshot.
These are added correctly, as soon as I remove the .HasColumnType("int(11) unsigned").
There is an additional issue with the type converter due to the issue tracked here https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql/issues/694
Further technical details
MySQL version: 5.7.24 Operating system: Win10 Pomelo.EntityFrameworkCore.MySql version: 2.1.5-preview1-20181129211431
Other details about my project setup:
This code in the migration generator code determines when AUTO_INCREMENT is output. Someone would need to debug and see why the autoIncrement flag isn't being set.
https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql/blob/e2bf0fd6e3b3c903d75993041bf26f541f7c0885/src/EFCore.MySql/Migrations/MySqlMigrationsSqlGenerator.cs#L913-L936
This part of the code fails, cause the MySqlValueGenerationStrategy.IdentityColumn value generation strategy isn't added within the migration file and therefore the code doesn't step into the if-branch. The question is, why does that happen, when I add such a custom column type?!
Have you tried [DatabaseGenerated(DatabaseGeneratedOption.Identity)] annotation? You said if you removed .HasColumnType("int(11) unsigned") it worked? Maybe also try .HasColumnType("int unsigned") instead (w/o display width).
Adding only .HasColumnType("int unsigned") adds the auto_increment correctly, but this is not acceptable to me, as I need to rebuild an existing database. My current workaround is to manually add .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn) each time, I add a new migration, which i luckily don't have to do that often.