1

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>();
    ...
}

1 Answer 1

1

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");
Sign up to request clarification or add additional context in comments.

Comments

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.