NDataMapper 1.0.2

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

NDataMapper

基于.NET6的以Ado .net Providers工作的一个简单ORM工具,扩展了IDbConnection接口。

使用方式

查询单行

返回单一类型、数组、对象模型

public static T? QueryFirst<T>(this IDbConnection conn, string sql, IDbTransaction? transaction = null, params IDbDataParameter[] paras);
public class People
{
    public int Id { get; set; }
    public string? Name { get; set; }
    public int? Age { get; set; }
    public decimal? Height { get; set; }
    public char? Gender { get; set; }
    public DateOnly? Birthday { get; set; }
    public DateTime? CreateTime { get; set; }
    public string[]? Arr1 { get; set; }
    public int[]? Arr2 { get; set; }
}

static void Test()
{
    string sql;
    using NpgsqlConnection conn = new("Server=localhost;Port=5432;userid=postgres;Password=postgres;Database=test;");

    //查询单个数据
    sql = $@"select name from public.people where id=:id";
    string? name = conn.QueryFirst<string>(sql, paras: new NpgsqlParameter("id", 1));

    //支持可空的值类型
    sql = $@"select age from public.people where id=:id";
    int? name = conn.QueryFirst<int?>(sql, paras: new NpgsqlParameter("id", 1));

    //支持数组
    sql = $@"select arr1 from public.people where id=:id";
    int[]? name = conn.QueryFirst<int[]>(sql, paras: new NpgsqlParameter("id", 1));

    //支持对象模型
    sql = $@"select * from public.people where id=:id";
    People? people = conn.QueryFirst<People>(sql, paras: new NpgsqlParameter("id", 1));
}

返回动态类型,可新增或者移除动态属性,大小写不敏感

public static dynamic? QueryFirst(this IDbConnection conn, string sql, IDbTransaction? transaction = null, params IDbDataParameter[] paras);
sql = $@"select * from public.people where id=:id";
var p = conn.QueryFirst(sql, paras: new NpgsqlParameter("id", 2));
if (p is not null)
{
    p.occupation = "程序员";//添加一组值
    p.Remove("id");//移除名为id动态属性
}

返回元组,最多支持2-5个

public static (Result1?, Result2?) QueryFirst<Result1, Result2>(this IDbConnection conn, string sql, IDbTransaction? transaction = null, params IDbDataParameter[] paras);
sql = $@"select name,age from public.people where id=:id";
var (name, age) = conn.QueryFirst<string, int?>(sql, paras: new NpgsqlParameter("id", 2));

查询集合

返回单一类型集合

public static IEnumerable<T?> Query<T>(this IDbConnection conn, string sql, IDbTransaction? transaction = null, params IDbDataParameter[] paras)
sql = $@"select name from public.people limit 15";
IEnumerable<string?> names = conn.Query<string>(sql);

sql = $@"select * from public.people limit 15";
IEnumerable<People?> peoples = conn.Query<People>(sql);

返回动态类型集合

public static IEnumerable<dynamic?> Query(this IDbConnection conn, string sql, IDbTransaction? transaction = null, params IDbDataParameter[] paras)
sql = $@"select * from public.people limit 15";
IEnumerable<dynamic?> peoples = conn.Query(sql);

自定义字段别名

支持.NET自带的字段特性System.ComponentModel.DataAnnotations.Schema.ColumnAttribute或者NDataMapper.NColumnAttribute

public class People
{
    public int Id { get; set; }
    [Column("realname")]
    //[NColumn("realname")]
    public string? Name { get; set; }
}
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  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.
  • net6.0

    • No dependencies.

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.2 600 5/7/2022
1.0.1 549 5/6/2022
1.0.0 551 5/6/2022