title: C# enum to postgres enum
body: <p>I am currently using postgres enum</p>
  <pre><code>CREATE TYPE http_action_enum AS ENUM ('GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH');
  CREATE TABLE IF NOT EXISTS response
  (
      id                          UUID                PRIMARY KEY,
      http_action                 http_action_enum    NOT NULL
  );
  </code></pre>
  <p>But when I using the ef framework to insert to postgres database I keep hit the below error:</p>
  <pre><code>Exception data:
  Severity: ERROR
  SqlState: 42804
  MessageText: column &quot;destination&quot; is of type source_dest_enum but expression is of type integer
  Hint: You will need to rewrite or cast the expression.
  </code></pre>
  <p>When I check the response data type it is actually the correct enum and not integer.</p>
  <p>Repository.cs</p>
  <pre><code>public async Task&lt;Response&gt; InsertRecord(Response response, CancellationToken cancellationToken)
  {
        await dBContext.response.AddAsync(response, cancellationToken).ConfigureAwait(true);
        await dBContext.SaveChangesAsync(cancellationToken).ConfigureAwait(true);
  
        return response;
  }
  </code></pre>
  <p>DBContext.cs</p>
  <pre><code>      protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
              modelBuilder.HasDefaultSchema(&quot;public&quot;);
              modelBuilder.HasPostgresEnum&lt;SourceDestinationEnum&gt;();
              modelBuilder.HasPostgresEnum&lt;HttpActionEnum&gt;();
  
              modelBuilder.Entity&lt;Response&gt;().Property(d =&gt; d.RespondedData).HasColumnType(&quot;json&quot;);
  </code></pre>
  <p>HttpActionEnum.cs</p>
  <pre><code>[JsonConverter(typeof(StringEnumConverter))]
      public enum HttpActionEnum
      {
          GET,
          POST,
      }
  </code></pre>
  <p>Does anyone come across mapping c# enum to postgres enum and could advice?</p>
  <p>I tried converting to an enum but it's failing with an error that say the column is of type enum but expression is of type text.</p>
  <p><a href="https://docs.microsoft.com/en-us/ef/core/modeling/value-conversions" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/ef/core/modeling/value-conversions</a></p>
  <p>DBContext.cs (updated)</p>
  <pre><code>      protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
              modelBuilder.HasDefaultSchema(&quot;public&quot;);
              modelBuilder.HasPostgresEnum&lt;SourceDestinationEnum&gt;();
              modelBuilder.HasPostgresEnum&lt;HttpActionEnum&gt;();
  
             modelBuilder.Entity&lt;Response&gt;(entity =&gt;
              {
                  entity.Property(e =&gt; e.RespondedData).HasColumnType(&quot;json&quot;);
                  entity.Property(e =&gt; e.Destination).HasConversion(
                      v =&gt; v.ToString(),
                      v =&gt; (SourceDestinationEnum)System.Enum.Parse(typeof(SourceDestinationEnum), v));
              });
  </code></pre>
  <p>Error</p>
  <pre><code>+       InnerException  {&quot;42804: column \&quot;destination\&quot; is of type source_dest_enum but expression is of type text&quot;}    System.Exception {Npgsql.PostgresException}
  </code></pre>