1

I've been struggling for two days with a PostgreSQL enum type issue in Entity Framework Core. Despite all my configuration, EF Core keeps trying to send a string (`text`) to an enum column.

The Problem

When calling SaveChangesAsync(), I get this error:

Npgsql.PostgresException (0x80004005): 42804: column "status" is of type application_status but expression is of type text

This is the code in my repository:

public async Task UpdateJobApplicationStatusAndReviewDate(
    JobApplication jobApplication,
    ApplicationStatus status,
    DateTime reviewedAt)
{
    if (jobApplication == null) 
        return;     

    jobApplication.ApplicationStatus = status;
    jobApplication.ReviewedAt = reviewedAt;

    await _context.SaveChangesAsync(); // ← FAILS HERE
}

Generated SQL (from EF Core logs)

 (Microsoft.EntityFrameworkCore.Database.Command)
  Executed DbCommand (20ms) [Parameters=[@p0='1', @p1='6', @p2='2025-11-17T00:00:00.0000000+03:00' (Nullable = true) (DbType = Date), @p3='1', @p6='1', @p4='approved' (Nullable = false), @p5='2025-11-17T00:00:00.0000000+03:00' (Nullable = true) (DbType = Date)], CommandType='Text', CommandTimeout='30']
  INSERT INTO employees (car_rental_id, client_id, hire_date, position_id)
  VALUES (@p0, @p1, @p2, @p3)
  RETURNING employee_id;
  UPDATE job_applications SET status = @p4, reviewed_at = @p5
  WHERE application_id = @p6;

It doesn't take into account INSERT INTO; there's no ::application_status cast → PostgreSQL rejects it.

Entity class:

public class JobApplication
{
    public int Id { get; set; }
    public ApplicationStatus ApplicationStatus { get; set; } = ApplicationStatus.pending;
    public DateTime? ReviewedAt { get; set; }
    // ...other props
}

Enum

public enum ApplicationStatus
{
    pending,
    approved,
    rejected
}

EF Core configuration (IEntityTypeConfiguration<JobApplication>)

builder.Property(ja => ja.ApplicationStatus)
       .HasColumnName("status")
       .HasColumnType("application_status")
       .HasConversion(
              v => v.ToString(),
              v => Enum.Parse(v) 
        )  // ← tried with and without, even tried just HasConvertion<string>() 
       .HasDefaultValue(ApplicationStatus.pending)
       .IsRequired();

OnModelCreating method:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);
    // Tried both:
    modelBuilder.HasPostgresEnum<ApplicationStatus>();
    modelBuilder.HasPostgresEnum<ApplicationStatus>(name: "application_status");
    modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
}

PostgreSQL schema

Enum values match C# enum

SELECT enum_range(null::application_status);  

    → { pending, approved, rejected }

Column in job_applications table in PostgreSQL:

ColumnTypeNullableDefaultstatusapplication_statusnot null'pending '::application_status

All configs matches with my Postgres database.

Additional note: querying works perfectly — no issues at all

This operation executes smoothly with zero errors:

public async Task<List<JobApplication>> GetPendingByRentalAsync(int carRentalId)
{
    return await _context.JobApplications
        .Where(ja => ja.CarRentalId == carRentalId && ja.ApplicationStatus == ApplicationStatus.pending)
        .ToListAsync(); 
}
New contributor
Мирон Никитин is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1
  • 2
    Which exact version of EF Core are you using? Did you use GlobalTypeMapper.MapEnum as documented? Alternatively the newer options.MapEnum npgsql.org/efcore/mapping/… Commented Nov 17 at 0:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.