https://github.com/Atulin/NpgsqlSourceGenerators
https://github.com/Atulin/NpgsqlSourceGenerators
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/Atulin/NpgsqlSourceGenerators
- Owner: Atulin
- License: mit
- Created: 2024-01-06T22:06:44.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2024-02-01T05:49:23.000Z (over 1 year ago)
- Last Synced: 2024-04-26T12:21:38.840Z (over 1 year ago)
- Language: C#
- Size: 73.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- csharp-source-generators - NpgsqlSourceGenerators -   Source Generator to automatically register marked enums with NpgSQL for use with PostgreSQL (Source Generators / Database / ORM)
README
[](https://www.nuget.org/packages/Atulin.NpgSqlSourceGenerator/)


[](/LICENSE)# Npgsql Source Generators
Registering all enums one by one is tedious. Use this.
## Usage
Place `[PostgresEnum]` attribute on the enums you want to register...
```cs
namespace MyCoolApp;[PostgresEnum]
public enum Status {
Completed,
InProgress,
Started,
Queued
}[PostgresEnum(Name = "process_priority")]
public enum Priority {
High,
Medium,
Low
}
```...and register them
```cs
var source = new NpgsqlDataSourceBuilder(connectionString).npgSourceBuilder
.MapPostgresEnums()
.Build();
services.AddDbContext(options => options.UseNpgsql(source));
```
```cs
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
```cs
builder.Services.AddDbContext(options => options.UseNpgsql(
"",
o => o.MapPostgresEnums()));
```