Rabbit.Common.Data.Dapper 1.3.1

dotnet add package Rabbit.Common.Data.Dapper --version 1.3.1
                    
NuGet\Install-Package Rabbit.Common.Data.Dapper -Version 1.3.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="Rabbit.Common.Data.Dapper" Version="1.3.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Rabbit.Common.Data.Dapper" Version="1.3.1" />
                    
Directory.Packages.props
<PackageReference Include="Rabbit.Common.Data.Dapper" />
                    
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 Rabbit.Common.Data.Dapper --version 1.3.1
                    
#r "nuget: Rabbit.Common.Data.Dapper, 1.3.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.
#:package Rabbit.Common.Data.Dapper@1.3.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Rabbit.Common.Data.Dapper&version=1.3.1
                    
Install as a Cake Addin
#tool nuget:?package=Rabbit.Common.Data.Dapper&version=1.3.1
                    
Install as a Cake Tool

Rabbit.Common.Data.Dapper

Dapper micro-ORM component supporting MySQL and SQL Server with transaction scope, stored procedures, and batch operations. Inject IDapperHelper.

Dapper 数据库访问组件,支持 SQL Server 和 MySQL。

安装

dotnet add package Rabbit.Common.Data.Dapper

注册服务

单数据库配置

using Rabbit.Common.Data.Dapper;

services.AddDapperConfigure(
    DapperDbType.MySql,
    "Server=localhost;Database=mydb;User=root;Password=123456;",
    saveQueryLog: false,
    saveCommandLog: false
);

多数据库配置

services.AddDapperMapConfigure(
    "mydb",
    DapperDbType.MySql,
    "Server=localhost;Database=mydb;User=root;Password=123456;"
);

services.AddDapperMapConfigure(
    "orderdb",
    DapperDbType.SqlServer,
    "Server=.;Database=orderdb;User=sa;Password=123456;"
);

使用方式

单数据库

public class UserService
{
    private readonly IDapperHelper _db;

    public UserService(IDapperHelper db)
    {
        _db = db;
    }

    public async Task<List<User>> GetUsersAsync()
    {
        return await _db.QueryAsync<User>("SELECT * FROM users");
    }
}

多数据库

using Microsoft.Extensions.DependencyInjection;

public class OrderService
{
    private readonly IDapperHelper _orderDb;
    private readonly IDapperHelper _logDb;

    public OrderService(
        [FromKeyedServices("orderdb")] IDapperHelper orderDb,
        [FromKeyedServices("logdb")] IDapperHelper logDb)
    {
        _orderDb = orderDb;
        _logDb = logDb;
    }
}

主要方法

查询方法

  • QueryAsync<T> - 查询列表
  • QueryFirstOrDefaultAsync<T> - 查询单个
  • ExecuteScalarAsync<T> - 查询单个值

执行方法

  • ExecuteAsync - 执行 SQL(INSERT/UPDATE/DELETE)
  • QueryProAsync<T> - 执行存储过程(查询)
  • ExecuteProAsync - 执行存储过程(命令)

CRUD 方法

  • Insert/InsertAsync - 插入单个实体
  • InsertBatch/InsertBatchAsync - 批量插入
  • Get/GetAsync - 根据 ID 获取
  • Update/UpdateAsync - 更新实体
  • Delete/DeleteAsync - 删除实体

事务支持

推荐方式:使用 BeginTransaction(自动管理连接)

// 使用 using 确保连接和事务正确释放
using var scope = _db.BeginTransaction();
try
{
    await _db.ExecuteAsync("UPDATE users SET balance = balance - 100 WHERE id = 1", transaction: scope.Transaction);
    await _db.ExecuteAsync("UPDATE users SET balance = balance + 100 WHERE id = 2", transaction: scope.Transaction);
    scope.Commit();
}
catch
{
    // 异常时自动回滚(Dispose 中处理)
    throw;
}

实体定义

使用 Dapper.Contrib 特性定义实体:

using Dapper.Contrib.Extensions;

[Table("users")]
public class User
{
    [Key]
    [ExplicitKey]  // 如果是自增主键使用 [Key],如果是手动指定使用 [ExplicitKey]
    public long Id { get; set; }

    public string Name { get; set; }

    public int Age { get; set; }
}

支持的数据库类型

  • DapperDbType.MySql - MySQL
  • DapperDbType.SqlServer - SQL Server

日志说明

  • 查询/命令执行时间超过 500ms 会自动记录慢查询日志
  • 可通过 saveQueryLogsaveCommandLog 参数强制记录所有日志
  • CRUD 操作日志会显示表名,便于调试
Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Rabbit.Common.Data.Dapper:

Package Downloads
Rabbit.Common.Full

Meta-package referencing all Rabbit.Common utility packages. / Rabbit.Common 全家桶,引用所有子包

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.3.1 103 5/28/2026
1.3.0 102 5/19/2026
1.2.1 112 4/7/2026
1.2.0 120 3/27/2026
1.1.0 140 3/12/2026
1.0.0 145 3/12/2026