Skip to content

WeihanLi/WeihanLi.EntityFramework

Repository files navigation

WeihanLi.EntityFramework

WeihanLi.EntityFramework

WeihanLi.EntityFramework Latest

Pipeline Build Status

Github Build Status

Intro

EntityFrameworkCore extensions that provide a comprehensive set of tools and patterns to enhance your Entity Framework Core development experience.

WeihanLi.EntityFramework offers:

  • Repository Pattern - Clean abstraction layer for data access
  • Unit of Work Pattern - Transaction management across multiple repositories
  • Automatic Auditing - Track all entity changes with flexible storage options
  • Auto-Update Features - Automatic handling of CreatedAt/UpdatedAt timestamps and user tracking
  • Soft Delete - Mark entities as deleted without physical removal
  • Database Extensions - Convenient methods for bulk operations and queries
  • Database Functions - SQL Server JSON operations and more

Quick Start

1. Installation

dotnet add package WeihanLi.EntityFramework

2. Basic Setup

// Program.cs
var builder = WebApplication.CreateBuilder(args);

// Add DbContext
builder.Services.AddDbContext<MyDbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));

// Add WeihanLi.EntityFramework services
builder.Services.AddEFRepository();
builder.Services.AddEFAutoUpdateInterceptor();
builder.Services.AddEFAutoAudit(auditBuilder =>
{
    auditBuilder.WithUserIdProvider<HttpContextUserIdProvider>()
                .WithStore<AuditDatabaseStore>();
});

var app = builder.Build();

3. Define Your Entities

public class Product : IEntityWithCreatedUpdatedAt, ISoftDeleteEntityWithDeleted
{
    public int Id { get; set; }
    public string Name { get; set; } = string.Empty;
    public decimal Price { get; set; }
    
    // Auto-update properties
    public DateTimeOffset CreatedAt { get; set; }
    public DateTimeOffset UpdatedAt { get; set; }
    
    // Soft delete property
    public bool IsDeleted { get; set; }
}

4. Use Repository Pattern

public class ProductService
{
    private readonly IEFRepository<MyDbContext, Product> _repository;

    public ProductService(IEFRepository<MyDbContext, Product> repository)
    {
        _repository = repository;
    }

    public async Task<Product> CreateProductAsync(string name, decimal price)
    {
        var product = new Product { Name = name, Price = price };
        return await _repository.InsertAsync(product);
        // CreatedAt/UpdatedAt automatically set, audit record created
    }

    public async Task<List<Product>> GetActiveProductsAsync()
    {
        return await _repository.GetListAsync(
            queryBuilder => queryBuilder.WithPredict(p => p.Price > 0)
        );
        // Soft deleted products automatically filtered out
    }
}

Package Release Notes

See Releases/PRs for details

Package Versions

For EF 8 and above, use 8.x or above major-version matched versions

For EF 7, use 3.x

For EF Core 5/6, use 2.x

For EF Core 3.x, use 1.5.0 above, and 2.0.0 below

For EF Core 2.x , use 1.4.x and below

Features

πŸ—οΈ Repository Pattern

  • IEFRepository<TDbContext, TEntity> - Generic repository interface
  • EFRepository - Full-featured repository implementation
  • EFRepositoryGenerator - Dynamic repository creation
  • Query Builder - Fluent API for complex queries
  • Bulk Operations - Efficient batch updates and deletes

πŸ”„ Unit of Work Pattern

  • IEFUnitOfWork<TDbContext> - Transaction management
  • Multi-Repository Transactions - Coordinate changes across entities
  • Rollback Support - Automatic error handling

πŸ“‹ Comprehensive Auditing

  • Automatic Change Tracking - Monitor all entity modifications
  • Flexible Storage - Database, file, console, or custom stores
  • Property Enrichment - Add custom metadata to audit records
  • User Tracking - Capture who made changes
  • Configurable Filtering - Include/exclude entities and properties

⚑ Auto-Update Features

  • Timestamp Management - Automatic CreatedAt/UpdatedAt handling
  • User Tracking - Automatic CreatedBy/UpdatedBy population
  • Soft Delete - Mark entities as deleted without removal
  • Custom Auto-Update - Define your own auto-update rules

πŸ”§ Database Extensions

  • Column Updates - Update specific columns only
  • Bulk Operations - Efficient mass updates
  • Query Helpers - Get table/column names, check database type
  • Paging Support - Built-in pagination for large datasets

πŸ—„οΈ Database Functions

  • JSON Support - JSON_VALUE for SQL Server 2016+
  • SQL Server Functions - Enhanced querying capabilities

Documentation

πŸš€ Getting Started Guide - Step-by-step setup instructions for new users

πŸ“– Complete Usage Guide - Comprehensive documentation with examples for all features

⚑ Advanced Features Guide - Custom interceptors, performance optimization, and integration patterns

πŸ“‹ Release Notes - Version history and breaking changes

πŸ”§ Sample Project - Working examples and demonstrations

Support

πŸ’‘ Questions? Check out the Usage Guide for detailed examples

πŸ› Found a bug or need help? Feel free to create an issue with reproduction steps

Usage

For detailed usage instructions, please refer to the Usage Documentation.