C
C#17mo ago
TeaFaris

❔ 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; }
//..
}
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; }
//..
}
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);
}
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?
3 Replies
TeaFaris
TeaFarisOP17mo ago
I tried to drop database, didn't work
Angius
Angius17mo ago
Might need the uuid-ossp extension
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server