I am currently using postgres enum
CREATE TYPE http_action_enum AS ENUM ('GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH');
CREATE TABLE IF NOT EXISTS response
(
id UUID PRIMARY KEY,
http_action http_action_enum NOT NULL
);
But when I using the ef framework to insert to postgres database I keep hit the below error:
Exception data:
Severity: ERROR
SqlState: 42804
MessageText: column "destination" is of type source_dest_enum but expression is of type integer
Hint: You will need to rewrite or cast the expression.
When I check the response data type it is actually the correct enum and not integer.
Repository.cs
public async Task<Response> InsertRecord(Response response, CancellationToken cancellationToken)
{
await dBContext.response.AddAsync(response, cancellationToken).ConfigureAwait(true);
await dBContext.SaveChangesAsync(cancellationToken).ConfigureAwait(true);
return response;
}
DBContext.cs
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("public");
modelBuilder.HasPostgresEnum<SourceDestinationEnum>();
modelBuilder.HasPostgresEnum<HttpActionEnum>();
modelBuilder.Entity<Response>().Property(d => d.RespondedData).HasColumnType("json");
HttpActionEnum.cs
[JsonConverter(typeof(StringEnumConverter))]
public enum HttpActionEnum
{
GET,
POST,
}
Does anyone come across mapping c# enum to postgres enum and could advice?
I tried converting to an enum but it's failing with an error that say the column is of type enum but expression is of type text.
https://learn.microsoft.com/en-us/ef/core/modeling/value-conversions
DBContext.cs (updated)
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("public");
modelBuilder.HasPostgresEnum<SourceDestinationEnum>();
modelBuilder.HasPostgresEnum<HttpActionEnum>();
modelBuilder.Entity<Response>(entity =>
{
entity.Property(e => e.RespondedData).HasColumnType("json");
entity.Property(e => e.Destination).HasConversion(
v => v.ToString(),
v => (SourceDestinationEnum)System.Enum.Parse(typeof(SourceDestinationEnum), v));
});
Error
+ InnerException {"42804: column \"destination\" is of type source_dest_enum but expression is of type text"} System.Exception {Npgsql.PostgresException}