Enigmatry.Entry.Core.EntityFramework 9.1.1-preview.4

This is a prerelease version of Enigmatry.Entry.Core.EntityFramework.
dotnet add package Enigmatry.Entry.Core.EntityFramework --version 9.1.1-preview.4
                    
NuGet\Install-Package Enigmatry.Entry.Core.EntityFramework -Version 9.1.1-preview.4
                    
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="Enigmatry.Entry.Core.EntityFramework" Version="9.1.1-preview.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Enigmatry.Entry.Core.EntityFramework" Version="9.1.1-preview.4" />
                    
Directory.Packages.props
<PackageReference Include="Enigmatry.Entry.Core.EntityFramework" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Enigmatry.Entry.Core.EntityFramework --version 9.1.1-preview.4
                    
#r "nuget: Enigmatry.Entry.Core.EntityFramework, 9.1.1-preview.4"
                    
#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.
#addin nuget:?package=Enigmatry.Entry.Core.EntityFramework&version=9.1.1-preview.4&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Enigmatry.Entry.Core.EntityFramework&version=9.1.1-preview.4&prerelease
                    
Install as a Cake Tool

Core EntityFramework Library

This library provides extension methods and utilities for Entity Framework Core, focusing on querying and data seeding.

Intended Usage

Use this library when you need additional query capabilities with Entity Framework Core, such as entity not found handling, mapping query results, and pagination support.

Installation

Add the package to your project:

dotnet add package Enigmatry.Entry.Core.EntityFramework

Usage Examples

Using Query Extensions

using Enigmatry.Entry.Core.EntityFramework;
using Enigmatry.Entry.Core.Entities;
using Enigmatry.Entry.Core.Paging;
using Microsoft.EntityFrameworkCore;
using System.Threading.Tasks;

// Sample entity class
public class Product 
{
    public int Id { get; set; }
    public string Name { get; set; } = string.Empty;
    public decimal Price { get; set; }
    public bool IsAvailable { get; set; }
}

public class ProductDto
{
    public int Id { get; set; }
    public string Name { get; set; } = string.Empty;
    public decimal Price { get; set; }
}

public class ProductService
{
    private readonly DbContext _dbContext;
    private readonly IMapper _mapper;
    
    public ProductService(DbContext dbContext, IMapper mapper)
    {
        _dbContext = dbContext;
        _mapper = mapper;
    }
    
    // Find a product by ID, throw exception if not found
    public async Task<Product> GetProductById(int id)
    {
        return await _dbContext.Set<Product>()
            .Where(p => p.Id == id)
            .SingleOrNotFoundAsync();
    }

    // Map a single entity to a DTO
    public async Task<ProductDto> GetProductDtoById(int id)
    {
        return await _dbContext.Set<Product>()
            .Where(p => p.Id == id)
            .SingleOrDefaultMappedAsync<Product, ProductDto>(_mapper);
    }
    
    // Map a collection of entities to DTOs
    public async Task<List<ProductDto>> GetAvailableProductDtos()
    {
        return await _dbContext.Set<Product>()
            .Where(p => p.IsAvailable)
            .ToListMappedAsync<Product, ProductDto>(_mapper);
    }
    
    // Get a paged response of products
    public async Task<PagedResponse<Product>> GetPagedProducts(int pageNumber, int pageSize)
    {
        var request = new PagedRequest
        {
            PageNumber = pageNumber,
            PageSize = pageSize,
            SortBy = "Name",
            SortDirection = "asc"
        };
        
        return await _dbContext.Set<Product>()
            .Where(p => p.IsAvailable)
            .ToPagedResponseAsync(request);
    }
}

Required Dependencies

This library builds on Entity Framework Core and has the following dependencies:

  1. Entity Framework Core
  2. System.Linq.Dynamic.Core - For dynamic sorting
  3. AutoMapper - For mapping entities to DTOs

Dependency Injection Example

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using AutoMapper;

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        // Register DbContext
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
            
        // Register AutoMapper (required for mapping extensions)
        services.AddAutoMapper(typeof(Startup).Assembly);
    }
}

Using the Seeding Interface

using Enigmatry.Entry.Core.EntityFramework.Seeding;
using Microsoft.EntityFrameworkCore;

// Create a seeder class
public class ProductSeeder : ISeeding
{
    public void Seed(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Product>().HasData(
            new Product 
            { 
                Id = 1, 
                Name = "Product 1", 
                Price = 19.99m, 
                IsAvailable = true 
            },
            new Product 
            { 
                Id = 2, 
                Name = "Product 2", 
                Price = 29.99m, 
                IsAvailable = true 
            },
            new Product 
            { 
                Id = 3, 
                Name = "Product 3", 
                Price = 39.99m, 
                IsAvailable = false 
            }
        );
    }
}

// Use the seeder in your DbContext
public class ApplicationDbContext : DbContext
{
    public DbSet<Product> Products { get; set; } = default!;
    
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }
    
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        
        // Apply the seeder
        new ProductSeeder().Seed(modelBuilder);
    }
}
Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Enigmatry.Entry.Core.EntityFramework:

Package Downloads
Enigmatry.Entry.EntityFramework

Building Block for adding EntityFramework related infrastructure to an Entry based project

Enigmatry.Entry.SmartEnums.EntityFramework

Building Block for support of SmartEnums with EntityFramework in an Entry based project

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
9.1.1-preview.4 80 6/27/2025
9.1.1-preview.3 125 6/4/2025
9.1.0 318 6/3/2025
9.0.1-preview.8 117 5/26/2025
9.0.1-preview.7 204 5/13/2025
9.0.1-preview.6 204 5/9/2025
9.0.1-preview.5 143 5/7/2025
9.0.1-preview.4 114 4/30/2025
9.0.1-preview.2 127 4/1/2025
9.0.0 945 2/26/2025
8.1.1-preview.3 124 5/7/2025
8.1.1-preview.1 127 4/1/2025
8.1.0 624 2/19/2025
8.0.1-preview.4 73 2/7/2025
8.0.1-preview.2 59 1/15/2025
8.0.0 1,185 11/27/2024
3.4.6-preview.10 71 11/27/2024
3.4.3 1,993 10/22/2024
3.4.2 724 10/11/2024
3.4.1 140 10/9/2024
3.4.0 140 10/9/2024
3.3.2 521 8/28/2024
3.3.2-preview.7 85 8/27/2024
3.3.1 426 7/16/2024
3.3.1-preview.4 71 7/12/2024
3.3.0 1,150 6/20/2024
3.2.1-preview.4 65 6/17/2024
3.2.1-preview.1 79 5/23/2024
3.2.0 3,895 4/3/2024
3.1.1-preview.1 1,030 3/13/2024
3.1.0 469 3/8/2024
3.1.0-preview.2 436 2/19/2024
3.0.1-preview.2 1,153 2/9/2024
3.0.1-preview.1 87 1/24/2024
3.0.0 1,230 1/15/2024
3.0.0-preview.14 117 1/9/2024
3.0.0-preview.12 82 1/9/2024
3.0.0-preview.5 77 1/10/2024
3.0.0-preview.2 144 12/28/2023
3.0.0-preview 160 12/20/2023
2.1.0 197 12/28/2023
2.0.1-preview.3 118 12/1/2023
2.0.1-preview.2 89 11/29/2023
2.0.1-preview.1 89 11/28/2023
2.0.0 474 11/8/2023
2.0.0-preview.3 453 10/27/2023
2.0.0-preview.2 95 10/27/2023
2.0.0-preview.1 87 10/27/2023
2.0.0-preview 149 10/27/2023
1.1.500 181 10/27/2023
1.1.495 233 9/24/2023
1.1.486 472 9/13/2023
1.1.484 235 9/7/2023
1.1.482 171 9/6/2023
1.1.480 322 8/24/2023
1.1.477 509 8/2/2023
1.1.464 215 7/5/2023
1.1.447 869 5/26/2023
1.1.396 315 4/11/2023
1.1.383 322 4/3/2023
1.1.377 309 3/13/2023
1.1.376 275 3/13/2023
1.1.365 1,004 2/15/2023