CustomDapperEntityActions 1.0.18

dotnet add package CustomDapperEntityActions --version 1.0.18
                    
NuGet\Install-Package CustomDapperEntityActions -Version 1.0.18
                    
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="CustomDapperEntityActions" Version="1.0.18" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CustomDapperEntityActions" Version="1.0.18" />
                    
Directory.Packages.props
<PackageReference Include="CustomDapperEntityActions" />
                    
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 CustomDapperEntityActions --version 1.0.18
                    
#r "nuget: CustomDapperEntityActions, 1.0.18"
                    
#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=CustomDapperEntityActions&version=1.0.18
                    
Install CustomDapperEntityActions as a Cake Addin
#tool nuget:?package=CustomDapperEntityActions&version=1.0.18
                    
Install CustomDapperEntityActions as a Cake Tool

CustomDapperEntityActions

A .NET package that provides Entity Framework Core-like actions using Dapper, designed for developers whose companies restrict the use of EF Core.

Overview

CustomDapperEntityActions bridges the gap between Dapper's lightweight performance and Entity Framework Core's convenient entity management features. It offers familiar entity action patterns while maintaining Dapper's speed and flexibility.

Features

  • EF Core-style entity operations
  • Built on top of Dapper for optimal performance
  • Compatible with .NET 9.0
  • Support for common CRUD operations

Installation

Install via NuGet Package Manager: dotnet add package CustomDapperEntityActions --version 1.0.6

Link to nuget: https://www.nuget.org/packages/CustomDapperEntityActions/

How to use

  1. Define entities
[Table("[schema].[table]")]
public class Plant
{
   [Key]
   [Column("id")]
   public int Id { get; set; }

   [Column("description")]
   public string? Description { get; set; }

   [Column("is_active")]
   public bool IsAcive { get; set; }
}

1.1 Define Entities with 1..n relationship

// Parent table
[Table("parent")]
public class Parent
{
    [Key]
    [Column("parent_id")]
    public int ParentId { get; set; }

    [Column("name")]
    public required string Name { get; set; }

    [NotMapped]
    public List<Child> Children { get; set; } = new List<Child>(); // initialise is important and needed 
}

// Child table
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

[Table("child")]
public class Child
{
    [Key]
    [Column("child_id")]
    public int ChildId { get; set; }

    // foreign key to parent necessary
    [Column("parent_id")]
    [ForeignKey("Parent")]
    public int ParentId { get; set; }

    [Column("name")]
    public required string Name { get; set; }

    [Column("is_active")]
    public bool IsActive { get; set; }
}
  1. Create DbContext
public class ApplicationDbContext : IDisposable
{
    // if using postgres, sql also possible
    private readonly NpgsqlConnection _connection;
    private IDbTransaction _transaction;

    // The entity we just made
    public DapperSet<Plant> Plants { get; }


    public ApplicationDbContext(string connectionString)
    {
        _connection = new NpgsqlConnection(connectionString);
        _connection.Open();
        _transaction = _connection.BeginTransaction();
        // assign them
        Plants = new DapperSet<Plant>(_connection, _transaction);
    }

    public void Commit() => _transaction?.Commit();
    public void Rollback() => _transaction?.Rollback();

    public void Dispose()
    {
        _transaction?.Dispose();
        _connection?.Dispose();
    }
}
  1. Update program.cs
bld.Services.AddScoped<ApplicationDbContext>(provider => new ApplicationDbContext(connectionString));
  1. usage
// insert
var insert = await dbContext.Tests.InsertAsync<string>(new TestLalala
{
    Description = "Test",
    IsAcive = true
});

// update
var update = await dbContext.Tests.UpdateAsync(new TestLalala
{
    Id = 5,
    Description = "Luksass",
    IsAcive = true
});
// Get byid
await dbContext.Tests.GetByIdAsync(5);
await dbContext.Tests.DeleteAsync(5);

// Linq like query NEW version
     var specificTests = await dbContext.Tests
         .Where(x => x.TestCd == "Zoko")
         .OrderBy(x => x.Description)
         .ExecuteAsync();
// without where also possible
 var specificTests = await dbContext.Tests
         .OrderBy(x => x.Description)
         .ExecuteAsync();
// only where also possible
     var specificTests = await dbContext.Tests
         .Where(x => x.TestCd == "Zoko")
// descending
 var specificTests = await dbContext.Tests
         .OrderBy(x => x.Description, descending: true)
         .ExecuteAsync();


// Chaining statements
 var specificTests = await dbContext.Tests
     .Where( x => x.TestMode == "Offline")
     .Where(x => x.IsActive )
     .OrderBy(x => x.TestType)
     .OrderBy( x => x.TestMode)
     .ExecuteAsync();



// Linq like query OLD version
var specificTests = await dbContext.Tests.WhereAsync(x =>x.IsAcive );

// GetAll
DbContext.Tests.GetAllAsync

Pagination

  var specificTests = await dbContext.Tests
           // Other linq queries
            .Paginate(0,50) // paginate(pageIndex, pageSize), this gives records 0-50 
            .ExecuteAsync();

Logging

If you want to log the queries, you need to put miminum log level to "Information" and use serilog.

JOIN

Joins can be done with the include tag (only one to one mapping currently one to many is for future features )

   var measurements = await dbContext.Measurements
          .Include(x => x.Test)
          .Include(x => x.Plant)
          // then you can also filter only on the parent
          .Where(x => x.Value > 100)
          .OrderBy(x => x.MeasurementDate)
          .Paginate(0, 2)
          .ExecuteAsync();

Classes need to be annotated like this with the NotMapped and ForeignKey tag

 [Table("ipc.measurement")]
public class CoolMeasurement
{
    [Key]
    [Column("id")]
    public int Id { get; set; }

    [Column("test_cd")]
    public required string TestCd { get; set; }

    [Column("plant_cd")]
    public required string PlantCd { get; set; }

    [Column("avg_value")]
    public double Value { get; set; }

    [Column("measurement_date")]
    public DateTime MeasurementDate { get; set; } = DateTime.UtcNow;

    [NotMapped]
    [ForeignKey("test_cd")]
    public TestLalala Test { get; set; }

    [NotMapped]
    [ForeignKey("plant_cd")]
    public Plant Plant { get; set; }
}

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. 
Compatible target framework(s)
Included target framework(s) (in 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.18 129 4/30/2025
1.0.16 128 4/30/2025
1.0.14 137 4/29/2025
1.0.13 103 4/25/2025
1.0.12-alpha.0.1 88 4/25/2025
1.0.11 101 4/25/2025
1.0.11-alpha.0.16.2.1 86 4/25/2025
1.0.11-alpha.0.16.2 83 4/25/2025
1.0.11-alpha.0.16 82 4/25/2025
1.0.11-alpha.0.15 82 4/25/2025
1.0.11-alpha.0.13 85 4/25/2025
1.0.11-alpha.0.11 85 4/25/2025
1.0.11-alpha.0.9 86 4/25/2025
1.0.11-alpha.0.8 124 4/24/2025
1.0.11-alpha.0.7 118 4/24/2025
1.0.10 152 4/23/2025
1.0.9 146 4/23/2025
1.0.8 180 4/14/2025
1.0.7 154 4/8/2025
1.0.6 152 4/8/2025
1.0.5 146 4/7/2025
1.0.4 123 4/4/2025
1.0.3 147 4/2/2025
1.0.2 141 4/1/2025
1.0.1 145 4/1/2025
1.0.0 142 4/1/2025
1.0.0-alpha.0.29 116 4/24/2025
0.0.0-alpha.0.28 124 4/23/2025