Aore.Dapper.Contrib 1.0.15

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

Aore.Dapper.Contrib — .NET SDK

Static Badge license

[English Version]

📈 一个基于Dapper实现一个简单的 .Net 对象映射器的通用工具库,提供了多种数据访问和实用功能的支持的开发组件,助力大量系统和应用取得成功。我将继续保持更新,并正在深度融合更多场景应用👐

使用 Aore.Dapper.Contrib,您可以方便快速将现有项目或新项目实现操作数据支持。

目前 AAore.Dapper.Contrib 支持 .NET Standard 2.0 等多种框架,同时兼容所有 MVC、Razor、WebApi、Console(命令行)、桌面应用(.exe)和后台服务等环境,和外部框架完全解耦。 适用于需要跨平台兼容性的应用程序开发。

希望有更多的人可以从中受益,理解并传播开源的精神,一同助力中国开源事业!感恩一路上给我们提供帮助的朋友们!

如果你喜欢并希望我们继续优化这个项目,请给我们一个 ★Star :)

✨ 功能特性

  • 多数据库支持:提供了对多种数据库(MySQL、Oracle、PostgreSQL、SQLite、SQL Server)的访问支持
  • 它不仅简化了数据库操作,还提高了代码的可维护性和可移植性,是开发高效、可靠数据库应用程序的理想选择
  • 包含一些辅助方法,用于插入、获取、更新和删除记录
  • ...

🚀 Aore.Dapper.Contrib 使用示例 :开启你的改造开发之旅!

.NET CLI:
dotnet add package Aore.Dapper.Contrib

或

Nuget包进行搜索下载

🛠 操作使用

扩展方法完整列表

T Get<T>(id);
T GetByWhere<T>(new {Name = "XXX"});
IEnumerable<T> GetAll<T>();
IEnumerable<T> GetAllById<T>(id);
IEnumerable<T> GetAllByWhere<T>(new {Name = "XXX"});
int Insert<T>(T obj);
int Insert<T>(Enumerable<T> list);
int InsertOra<T>(T obj);
bool Update<T>(T obj);
bool Update<T>(T obj, new {Name = "XXX"});
bool UpdateByWhere<T>(new {Name = "NewValue"}, new {Id = "XXX"});
bool Update<T>(Enumerable<T> list);
bool Delete<T>(T obj);
bool Delete<T>(Enumerable<T> list);
bool DeleteByWhere<T>(new {Name = "XXX"});
bool DeleteAll<T>();

为了使这些扩展程序正常工作,相关实体必须具有一个键属性。如果存在名为"id"的属性(不区分大小写),Dapper将自动使用它作为键属性。

public class Car
{
    public int Id { get; set; } // Works by convention
    public string Name { get; set; }
}

如果实体不遵循此约定,请使用 [Key] 属性来修饰特定属性。

public class User
{
    [Key]
    int TheId { get; set; }
    string Name { get; set; }
    int Age { get; set; }
}

Get 方法

根据ID获取特定实体

var car = connection.Get<Car>(1);

表中的所有实体的列表

var cars = connection.GetAll<Car>();

Insert 方法

插入一个实体

connection.Insert(new Car { Name = "Volvo" });

或一个实体列表

connection.Insert(cars);

Update 方法

更新单个实体

connection.Update(new Car() { Id = 1, Name = "Saab" });

或更新实体列表

connection.Update(cars);

Delete 方法

通过指定的 [Key] 属性删除实体

connection.Delete(new Car() { Id = 1 });

实体列表

connection.Delete(cars);

表中的所有实体

connection.DeleteAll<Car>();

特殊属性

Aore.Dapper.Contrib 使用了一些可选的属性:

  • [Table("Tablename")] - 使用另一个表名,而不是默认的复数形式的类名

    [Table ("emps")]
    public class Employee
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    
  • [Key] - 此属性代表数据库生成的标识符/键

    public class Employee
    {
        [Key]
        public int EmployeeId { get; set; }
        public string Name { get; set; }
    }
    
  • [Write(true/false)] - 此属性是(不)可写的

  • [Computed] - 此属性为计算得出,不应包含在更新中

  • [Column] - 数据库列名

限制和注意事项

SQLite

SQLiteConnection 暴露了一个与 Dapper.Contrib 提供的 Update 扩展冲突的 Update 事件。有 2 种方法可以处理这个问题。

  1. 从 SqlMapperExtensions 显式调用 Update 方法

    SqlMapperExtensions.Update(_conn, new Employee { Id = 1, Name = "Mercedes" });
    
  2. 通过向 Update 传递类型参数使方法签名唯一

    connection.Update<Car>(new Car() { Id = 1, Name = "Maruti" });
    

✋ 贡献代码

如果需要使用或修改此项目的源代码,建议先Fork。也欢迎将您修改的通用版本Pull Request过来。

  1. Fork
  2. 创建您的特性分支 (git checkout -b my-new-feature)
  3. 提交您的改动 (git commit -am 'Added some feature')
  4. 将您的修改记录提交到远程 git 仓库 (git push origin my-new-feature)
  5. 然后到 github 网站的该 git 远程仓库的 my-new-feature 分支下发起 Pull Request (请提交到 Developer 分支,不要直接提交到 master 分支)
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  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 is compatible.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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.