SZORM 1.0.4

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

SZORMCore - 轻量级 .NET ORM 框架核心引擎

.NET Version License NuGet Version

SZORMCore 是一个轻量级、高性能的 .NET ORM(对象关系映射)框架核心引擎,提供简洁的 API 和强大的功能特性。支持 SQL Server、MySQL、PostgreSQL、Oracle、SQLite 及 OceanBase/GoldenDB/PolarDB 共 8 种数据库类型。

✨ 功能特性

核心功能

  • 简洁的 API 设计 - 类似 Entity Framework 的流畅 API
  • 强大的实体映射 - 基于 Attribute 的约定配置
  • 自动化 SQL 生成 - 无需手写 SQL,自动生成增删改查语句
  • 事务支持 - 完整的 ACID 事务管理
  • 连接池管理 - 高效的数据库连接复用机制

高级功能

  • 🔐 字段级加密存储 - 支持 SM4/AES 加密算法
  • 🔍 哈希索引支持 - 支持加密字段的模糊查询
  • 📊 复杂查询支持 - 支持 Lambda 表达式查询
  • 🎯 主键自动生成 - 内置 SnowID 分布式 ID 生成器
  • 📝 SQL 日志 - 完整的 SQL 执行日志记录

📁 项目依赖

SZORM.Core (本包)
    ↓ 引用
SZORM.Attribute (属性定义库)
    ↓ 零依赖

🚀 快速开始

1. 安装

dotnet add package SZORM --version 1.0.2

或直接在 .csproj 中添加:

<ItemGroup>
  <PackageReference Include="SZORM" Version="1.0.2" />
</ItemGroup>

2. 定义实体类

using SZORM.Attribute;
using System;

[SZTable("Users")]  // 映射到 Users 表
public class User
{
    [SZColumn(IsPrimaryKey = true)]  // 主键
    public long Id { get; set; }

    [SZColumn(IsNotNull = true, MaxLength = 50)]  // 非空,最大长度 50
    public string Name { get; set; }

    [SZColumn(IsEncrypt = true, EncryptMode = EncryptMode.SM4)]  // 加密存储
    public string IdCard { get; set; }
}

3. 创建 DbContext

构造函数接收的是 szorm.json 中的配置段名(而非原始连接字符串)。无参构造使用类名作为配置段名。

using SZORM.Core;

public class MyDbContext : DbContext
{
    public MyDbContext() : base("MyDb")   // 对应 szorm.json 里的 "MyDb" 配置段
    {
    }

    public DbSet<User> Users { get; set; }
    public DbSet<Order> Orders { get; set; }
}

4. 基本 CRUD 操作

using (var db = new MyDbContext())
{
    // 添加数据
    var user = new User
    {
        Name = "张三",
        IdCard = "110101199001011234"  // 自动加密存储
    };
    db.Users.Add(user);
    db.Commit();

    // 查询数据(通过 AsQuery() 进入 Lambda 查询)
    var users = db.Users.AsQuery().Where(u => u.Name.Contains("张")).ToList();

    // 更新数据
    user.Name = "李四";
    db.Users.Edit(user);
    db.Commit();

    // 删除数据
    db.Users.Remove(user);
    db.Commit();
}

📚 详细功能文档

详细功能文档请参考主项目 README:

功能速查表

功能 说明
实体映射 基于 [SZTable] / [SZColumn] 的约定式映射
查询功能 AsQuery() + Lambda:Where / OrderBy / GroupBy / Join / 聚合
插入功能 Add / AddRange / Add(Expression) 及异步版本
更新功能 Edit(entity) / Edit(condition, body)
删除功能 Remove / RemoveHard(软删除 / 物理删除)
事务管理 BeginTran / Commit(Save) / Rollback 及异步版本
加密存储 SM4 / AES + 随机 IV + 搜索哈希列
导航属性 Include / ThenInclude 急加载 + 懒加载代理
SQL 日志 注入 ILogger 记录 SQL 与耗时

⚙️ 配置说明

szorm.json 配置

{
  "MyDb": {
    "type": "MySql",
    "connStr": "Server=127.0.0.1;Database=test;User Id=root;Password=pwd;charset=utf8mb4;pooling=false",
    "workerId": "1",
    "useLazyLoadingProxies": "true",
    "sm4Key": "0123456789abcdeffedcba9876543210",
    "aesKey": "0123456789abcdef0123456789abcdef",
    "hashKey": "0123456789abcdef0123456789abcdef",
    "hashMode": "SM3"
  }
}

配置以命名段组织:{段名}:type 为数据库类型,{段名}:connStr 为连接串。支持的 typeMySql / Sqlite / Oracle / SqlServer / PostgreSQL / OceanBase(GoldenDB、PolarDB 同用 OceanBase 提供程序)。

详细配置说明请参考:SZORMCore 主项目 README

🚀 性能优化

1. 连接池配置

{
  "maxPoolSize": 200,
  "minPoolSize": 10,
  "connectionLifetime": 300
}

2. 批量操作

// 使用 AddRange 批量插入
var users = new List<User>();
for (int i = 0; i < 1000; i++)
{
    users.Add(new User { Name = $"User{i}", Age = i % 100 });
}
db.Users.AddRange(users);
db.Commit();

详细性能优化建议请参考:主项目 README · 性能优化

📝 最佳实践

1. 使用 using 语句管理资源

// ✅ 推荐
using (var db = new MyDbContext())
{
    var users = db.Users.ToList();
}

2. 及时提交事务

using (var db = new MyDbContext())
{
    db.Users.Add(new User { Name = "张三" });
    db.Commit();  // 及时提交
}

详细最佳实践请参考:主项目 README · 最佳实践

❓ 常见问题

1. 如何映射视图?

[SZTable("UserView")]
public class UserView
{
    [SZColumn(IsPrimaryKey = true)]
    public long Id { get; set; }
    public string UserName { get; set; }
    public string OrderNo { get; set; }
}

更多常见问题请参考:主项目 README · 常见问题

📄 许可证

本项目采用 MIT 许可证。详见 LICENSE 文件。

🤝 贡献

欢迎提交 Issue 和 Pull Request!

📧 联系方式

如有问题或建议,请提交 Issue 或联系项目维护者。


⚡ 相关链接

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

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.4 63 8/31/2026
1.0.3 68 8/27/2026
1.0.2 109 8/15/2026
1.0.1 118 7/31/2026
1.0.0 173 6/11/2026