4

I'm writing a middleware that logs the exceptions I get.

I created enum values ​​on postgresql

enter image description here

and mapped this to my log table

enter image description here

My Log model is:

   public class Log
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [JsonIgnore]
    public int Id { get; set; }
    public EventType EventType { get; set; }
    public string Description { get; set; }
    public DateTime CreatedAt { get; set; }

}

My Error middleware is:

        var log = new Log();
        log.Description = ex.Message;
        log.EventType = EventType.Exception;
        log.CreatedAt = DateTime.Now;

        Context.Add(log);
        Context.SaveChanges();

(This is coming on interfaces, but I wanted to show shortly)

My Context:

 protected override void OnModelCreating(ModelBuilder model)
    {
        model
            .HasPostgresEnum(null, "event_type", new[] { "info", "unknown", "warning", "exception" });


  protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder
            .UseNpgsql(Configuration.GetConnectionString("MyConnection"))
            .UseCamelCaseNamingConvention();
        NpgsqlConnection.GlobalTypeMapper.MapEnum<EventType>("enum_logs_eventType");
    }

i think i did everything right. It is DB First, so I migrated the database, but I think you did the same steps there.

when i run the service and trigger any error i get this error:

enter image description here

When I try to cast to string or run .HasConversion or something i get this error:
 

stringError

2
  • 1
    EventType.Exception value is obviously 'exception' while Posthresql enum_logs_eventType contains 'Exception'. Posthresql enum values are case-sensitive. Hence invalid input value for enum "enum_logs_eventType": "exception". Commented Feb 11, 2021 at 22:33
  • this information solved my problem. I deleted and recreated enum value. Can you write this as the answer? If anyone have a this error, they may not see this comment. Commented Feb 11, 2021 at 23:40

1 Answer 1

3

Posthresql enum values ​​are case sensitive. So when I deleted the enums and fixed them again (they're all lowercase), the error was resolved.

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.