Atulin.NpgSqlSourceGenerator 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Atulin.NpgSqlSourceGenerator --version 1.0.1                
NuGet\Install-Package Atulin.NpgSqlSourceGenerator -Version 1.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Atulin.NpgSqlSourceGenerator" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Atulin.NpgSqlSourceGenerator --version 1.0.1                
#r "nuget: Atulin.NpgSqlSourceGenerator, 1.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Atulin.NpgSqlSourceGenerator as a Cake Addin
#addin nuget:?package=Atulin.NpgSqlSourceGenerator&version=1.0.1

// Install Atulin.NpgSqlSourceGenerator as a Cake Tool
#tool nuget:?package=Atulin.NpgSqlSourceGenerator&version=1.0.1                

NuGet Version NuGet Downloads GitHub Actions Workflow Status

Npgsql Source Generators

Registering all enums one by one is tedious. Use this.

Usage

namespace MyCoolApp;

[PostgresEnum]
public enum Status {
    Completed,
    InProgress,
    Started,
    Queued
}

[PostgresEnum(Name = "process_priority")]
public enum Priority {
    High,
    Medium,
    Low
}

will generate the following code:

// <auto-generated/>
#nullable enable
using Npgsql;
using Microsoft.EntityFrameworkCore;
       
namespace NpgSqlGenerators;

internal static class PostgresEnumHelpers
{
    /// <summary>
    /// Calls <see cref="Npgsql.NpgsqlDataSourceBuilder.MapEnum{T}"/> on selected enums
    /// </summary>
    public static NpgsqlDataSourceBuilder MapPostgresEnums(this NpgsqlDataSourceBuilder builder)
    {
        builder.MapEnum<MyCoolApp.Status>();
        builder.MapEnum<MyCoolApp.Priority>(pgName: "process_priority");
        return builder;
    }

    /// <summary>
    /// Calls <see cref="Microsoft.EntityFrameworkCore.NpgsqlModelBuilderExtensions.HasPostgresEnum{T}"/> on selected enums
    /// </summary>
    public static void RegisterPostgresEnums(this ModelBuilder builder)
    {
        builder.HasPostgresEnum<MyCoolApp.Status>();
        builder.HasPostgresEnum<MyCoolApp.Priority>(name: "process_priority");
    }
}

which can be used, for example, like so:

var source = new NpgsqlDataSourceBuilder(connectionString).npgSourceBuilder
    .MapPostgresEnums()
    .Build();
services.AddDbContext<ApplicationDbContext>(options => options.UseNpgsql(source));
public class MyDbContext : DbContext
{
    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        builder.RegisterPostgresEnums();
    }    
}
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.2 785 2/1/2024
1.0.1 740 2/1/2024
1.0.0 1,071 1/16/2024