entity-frameworknpgsql

Newest version of npgsql with Entity Framework casting enum type error


In Postgresql 16 I have a custom enum type schema.status. I updated the npgsql package to the latest version and modified code that was broken due to the new way of mapping enums.

Entity Framework queries that used to work are getting translated with the cast that produces error.

var pendingOrderCheck = _data.Orders.GetFirstOrDefault(w => w.NonPublicSchoolId == schoolId && w.Status == Status.PENDING) == null ? false : true;

This gets translated to:

SELECT TOP(1) 
    o.id, o.bookbudgetcode, o.comments, o.nonpublicschoolid, 
    o.pogenerated, o.ponumber, o.shipbudgetcode, o.statuscode,  
    o.updatetime, o.updateuser, o.usedbookorder, o.usedok, 
    o.vendorid
FROM 
    textb_2025_149100900000.orders AS o
WHERE 
    (o.nonpublicschoolid == @__schoolId_0) 
    && (**o.statuscode == 'PENDING'::"schema.status"**))

Which produces this error:

SqlState: 42704
MessageText: type "schema.status" does not exist

But the enum schema.status does exist.

As directed mapped the enum now to:

services.AddDbContext<TBContext>(o => o.UseNpgsql(dbConnectionString, o => o.MapEnum<Status>("schema.status")));

Solution

  • You're passing schema.status as a single string to MapEnum's name parameter, which means that the enum really is supposed to be called schema.status (as opposed to an enum named status in the schema schema. EFCore.PG is surrounding that name in double quotes, which is the correct thing to do.

    If you want to map to an enum named status in the schema schema, you need to call the MapEnum overload that accepts the name and schema as separate parameters:

    o.MapEnum<Status>("status", "schema")