C#C
C#3y ago
Faris

❔ EF Postgres migration fail

So, I recently changed my model primary key from int to Guid.
Before:
public class File
{
  [Key]
  public int Id { get; set; }
  //...
}
public class User
{
  //...
  public int? ProfilePictureId { get; set; }
  [ForeignKey(nameof(ProfilePictureId))]
  public File? ProfilePicture { get; set; }
  //..
}

After:
public class File
{
  [Key]
  public Guid Id { get; set; }
  //...
}
public class User
{
  //...
  public Guid? ProfilePictureId { get; set; }
  [ForeignKey(nameof(ProfilePictureId))]
  public File? ProfilePicture { get; set; }
  //..
}
and after executing add-migration file-id-change-guid it created a migraiton file:
protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.AlterColumn<Guid>(
        name: "Id",
        table: "Files",
        type: "uuid",
        nullable: false,
        oldClrType: typeof(int),
        oldType: "integer")
        .OldAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);

    migrationBuilder.AlterColumn<Guid>(
        name: "ProfilePictureId",
        table: "AspNetUsers",
        type: "uuid",
        nullable: true,
        oldClrType: typeof(int),
        oldType: "integer",
        oldNullable: true);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
    migrationBuilder.AlterColumn<int>(
        name: "Id",
        table: "Files",
        type: "integer",
        nullable: false,
        oldClrType: typeof(Guid),
        oldType: "uuid")
        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);

    migrationBuilder.AlterColumn<int>(
        name: "ProfilePictureId",
        table: "AspNetUsers",
        type: "integer",
        nullable: true,
        oldClrType: typeof(Guid),
        oldType: "uuid",
        oldNullable: true);
}
But now it's throwing me a 22023: identity column type must be smallint, integer, or bigint. Any ideas?
Was this page helpful?