© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
3 replies
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; }
  //..
}
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
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
22023: identity column type must be smallint, integer, or bigint
. Any ideas?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

❔ ef migration snapshots
C#CC# / help
4y ago
EF Migration error
C#CC# / help
4y ago
Migration issues -EF core.
C#CC# / help
16mo ago
EF migration from VScode ?
C#CC# / help
3y ago