Although the answer given by @Shay Rojansky is correct, I encountered some difficulties when trying to apply it as described above. I had to adjust it a bit to make it work for me:
Let say we have an entity like this:
public class Service
{
public long Id { get; set; }
public string? Name { get; set; }
public string? Image { get; set; }
}
And you decided to change the type of "Image" from "string" to "byte[]" (or from "byte" to "byte[]") and created your migrations after the changes. PostgreSQL will throw an exception stating that "column cannot be automatically converted to type bytea"
As @Shay Rojansky already pointed out, you should go through your migrations file to find something like the following:
migrationBuilder.AlterColumn<byte[]>(
name: "Image",
table: "Services",
type: "bytea",
nullable: true,
oldClrType: typeof(string),
oldType: "text");
If you find it, you need to replace it with:
migrationBuilder.Sql(@"ALTER TABLE ""Services"" ALTER COLUMN ""Image"" TYPE BYTEA USING ""Image""::bytea");
I used the verbatim string literal to escape special characters so that the resulting string is the following in pure SQL:
ALTER TABLE "Services" ALTER COLUMN "Image" TYPE BYTEA USING "Image"::bytea;
For more details, please see these pages:
up
method of the migration file.migrationBuilder.AlterColumn<byte[]>( name: "Logo", table: "Companies", nullable: true, oldClrType: typeof(byte), oldNullable: true);
– Horary