ConvergeERP.Shared.Data.PostgreSQL
2.0.1
dotnet add package ConvergeERP.Shared.Data.PostgreSQL --version 2.0.1
NuGet\Install-Package ConvergeERP.Shared.Data.PostgreSQL -Version 2.0.1
<PackageReference Include="ConvergeERP.Shared.Data.PostgreSQL" Version="2.0.1" />
<PackageVersion Include="ConvergeERP.Shared.Data.PostgreSQL" Version="2.0.1" />
<PackageReference Include="ConvergeERP.Shared.Data.PostgreSQL" />
paket add ConvergeERP.Shared.Data.PostgreSQL --version 2.0.1
#r "nuget: ConvergeERP.Shared.Data.PostgreSQL, 2.0.1"
#:package ConvergeERP.Shared.Data.PostgreSQL@2.0.1
#addin nuget:?package=ConvergeERP.Shared.Data.PostgreSQL&version=2.0.1
#tool nuget:?package=ConvergeERP.Shared.Data.PostgreSQL&version=2.0.1
ConvergeERP.Shared.Data.PostgreSQL
Shared data services for PostgreSQL database with Entity Framework Core. Provides a generic repository pattern implementation.
Features
- Repository Pattern: Generic repository implementation for CRUD operations
- PostgreSQL Support: Built on Npgsql.EntityFrameworkCore.PostgreSQL
- Pagination Support: Built-in paged list queries
- Transaction Support: Easy transaction management
- No External Dependencies: Works with any DbContext without requiring additional authorization packages
Installation
dotnet add package ConvergeERP.Shared.Data.PostgreSQL
Quick Start
1. Create Your DbContext
public class MyDbContext : DbContext
{
public MyDbContext(DbContextOptions<MyDbContext> options) : base(options) { }
public DbSet<Product> Products { get; set; }
public DbSet<Order> Orders { get; set; }
}
2. Register Services
// In Program.cs
builder.Services.AddDbContext<MyDbContext>(options =>
{
options.UseNpgsql(connectionString);
});
// Register repository
builder.Services.AddScoped(typeof(IRepository<,>), typeof(Repository<,>));
3. Use the Repository
public class ProductService
{
private readonly IRepository<MyDbContext, Product> _repository;
public ProductService(IRepository<MyDbContext, Product> repository)
{
_repository = repository;
}
public async Task<List<Product>?> GetAllProductsAsync()
{
return await _repository.GetAllAsync();
}
public async Task<Product> CreateProductAsync(Product product)
{
return await _repository.AddAsync(product);
}
}
Repository Methods
Read Operations
// Get all with optional ordering and includes
var products = await repository.GetAllAsync(
orderBy: q => q.OrderBy(p => p.Name),
includeProperties: "Category,Supplier");
// Get filtered
var activeProducts = await repository.GetByAsync(
selector: p => p.IsActive,
orderBy: q => q.OrderByDescending(p => p.CreatedAt));
// Get single
var product = await repository.GetSingleAsync(
selector: p => p.Id == productId,
includeProperties: "Category");
// Get paged
var pagedProducts = repository.GetPagedListBy(
new QueryOptions { PageNumber = 1, PageSize = 20 },
selector: p => p.IsActive);
Write Operations
// Add
repository.Add(product);
await repository.SaveChangesAsync();
// Or add and save in one call
var created = await repository.AddAsync(product);
// Add multiple
repository.AddRange(products);
await repository.SaveChangesAsync();
// Update
repository.Update(product);
await repository.SaveChangesAsync();
// Update multiple
repository.UpdateRange(products);
await repository.SaveChangesAsync();
// Delete
repository.Remove(product);
await repository.SaveChangesAsync();
// Delete multiple
repository.RemoveRange(products);
await repository.SaveChangesAsync();
Transactions
using var transaction = repository.BeginTransaction();
try
{
repository.Add(product);
repository.Add(inventory);
await repository.SaveChangesAsync();
await transaction.CommitAsync();
}
catch
{
await transaction.RollbackAsync();
throw;
}
Using with DatabaseProvider
You can also use the PostgreSQLProvider for automatic setup:
// Register using the provider
var provider = new PostgreSQLProvider();
provider.AddDatabase<MyDbContext>(services, connectionString);
This will:
- Register the DbContext with PostgreSQL
- Register the generic repository
- Run pending migrations automatically
Query Options
For paged queries, use the QueryOptions class:
var options = new QueryOptions
{
PageNumber = 1,
PageSize = 20
};
var pagedResult = repository.GetPagedListBy(options, p => p.IsActive);
// Access pagination info
Console.WriteLine($"Total: {pagedResult.TotalCount}");
Console.WriteLine($"Pages: {pagedResult.TotalPages}");
Console.WriteLine($"Has Next: {pagedResult.HasNextPage}");
No-Tracking Queries
All read operations support no-tracking mode for better performance when you don't need to modify entities:
// Default is noTracking = true
var products = await repository.GetAllAsync(noTracking: true);
// Use noTracking = false when you need to update entities
var product = await repository.GetSingleAsync(
p => p.Id == id,
noTracking: false);
product.Name = "Updated";
await repository.SaveChangesAsync();
Entity Requirements
Entities can be any class. For entities that inherit from GlobalBaseEntity, you get additional audit fields:
public class Product : GlobalBaseEntity
{
public string Name { get; set; }
public decimal Price { get; set; }
}
// GlobalBaseEntity includes:
// - Id (Guid)
// - CreatedAt, CreatorId
// - UpdatedAt, UpdaterId
// - DeletedAt, DeleterId
// - Version
For multi-tenant entities, use BaseEntity:
public class Product : BaseEntity
{
public string Name { get; set; }
public decimal Price { get; set; }
}
// BaseEntity includes all GlobalBaseEntity fields plus:
// - TenantId (Guid)
// - CompanyId (Guid)
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- ConvergeERP.Shared.Data (>= 1.0.4)
- ConvergeERP.Shared.Domain (>= 2.0.4)
- Npgsql.EntityFrameworkCore.PostgreSQL (>= 10.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.