admin管理员组

文章数量:1023538

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, oments, 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")));

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, oments, 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")));
Share Improve this question edited Nov 19, 2024 at 4:48 marc_s 757k184 gold badges1.4k silver badges1.5k bronze badges asked Nov 19, 2024 at 4:30 user22523049user22523049 32 bronze badges 1
  • seems that EF adds " to the type name that causes the error as Postgres looks for the literal type "schema.status". The question is how to prevent it. – user22523049 Commented Nov 19, 2024 at 14:20
Add a comment  | 

1 Answer 1

Reset to default 0

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")

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, oments, 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")));

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, oments, 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")));
Share Improve this question edited Nov 19, 2024 at 4:48 marc_s 757k184 gold badges1.4k silver badges1.5k bronze badges asked Nov 19, 2024 at 4:30 user22523049user22523049 32 bronze badges 1
  • seems that EF adds " to the type name that causes the error as Postgres looks for the literal type "schema.status". The question is how to prevent it. – user22523049 Commented Nov 19, 2024 at 14:20
Add a comment  | 

1 Answer 1

Reset to default 0

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")

本文标签: Newest version of npgsql with Entity Framework casting enum type errorStack Overflow