Registering all enums one by one is tedious. Use this.
Place [PostgresEnum]
attribute on the enums you want to register...
namespace MyCoolApp;
[PostgresEnum]
public enum Status {
Completed,
InProgress,
Started,
Queued
}
[PostgresEnum(Name = "process_priority")]
public enum Priority {
High,
Medium,
Low
}
...and register them
var source = new NpgsqlDataSourceBuilder(connectionString).npgSourceBuilder
.MapPostgresEnums()
.Build();
services.AddDbContext<MyDbContext>(options => options.UseNpgsql(source));
public class MyDbContext : DbContext
{
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.RegisterPostgresEnums();
}
}
Tip
In Npgsql 9.0, you can use just this instead of the previous two
builder.Services.AddDbContext<MyContext>(options => options.UseNpgsql(
"<connection string>",
o => o.MapPostgresEnums()));