Abdelhakim
Abdelhakim

Reputation: 1164

Entity framework core postgres enum type does not exist

I have a column in postgres of type enum (name of the enum/type in the database : document_validity).

Entity framework generates the following (part of the) code when I query the column:

...
SELECT CASE
WHEN d3."Validity" IN ('not_checked'::document_validity, 'rejected'::document_validity) THEN 4
ELSE 1
...

This throws an exception saying

Npgsql.PostgresException (0x80004005): 42704: type "document_validity" does not exist

I'm able to make the query manually work by adding the name of the schema before the name of the enum type (name of the schema : "Main"):

SELECT CASE
WHEN d3."Validity" IN ('not_checked'::"Main".document_validity, 'rejected'::"Main".document_validity) THEN 4
ELSE 1

How can I tell entity framework to prefix the enum type with the schema name ?

I'm doing code first (if that's important). Here is the model:

public enum DocumentValidity
{
    NotChecked = 0,
    Validated = 1,
    Rejected = 2,
}

public class Document
{
    ...
    public DocumentValidity Validity { get; set; }
    ...
}

public class MyDbContext : DbContext
{
    ...
    static MyDbContext()
        => NpgsqlConnection.GlobalTypeMapper.MapEnum<DocumentValidity>();

    protected override void OnModelCreating(ModelBuilder builder)
        => builder.HasPostgresEnum<DocumentValidity>();
    ...
}

Upvotes: 1

Views: 1323

Answers (1)

Abdelhakim
Abdelhakim

Reputation: 1164

I figured it out. Since my schema is not the default public schema, I had to add it to the MapEnum call.

static MyDbContext() => NpgsqlConnection.GlobalTypeMapper.MapEnum<DocumentValidity>("Main.document_validity");

Upvotes: 1

Related Questions