NET8 ORM 使用AOT SqlSugar

.NET AOT8 基本上能够免强使用了, SqlSugar ORM也支持了CRUD 能在AOT下运行了

Nuget安装

复制代码
SqlSugarCore

具体代码

复制代码
StaticConfig.EnableAot = true;//启用AOT 程序启动执行一次就好了
 
//用SqlSugarClient每次都new,不要用单例模式 
var db = new SqlSugarClient(new ConnectionConfig()
   {
       IsAutoCloseConnection = true,
       DbType = DbType.Sqlite,
       ConnectionString = "datasource=demo.db" 
 
   },
   it =>
   {
       // Logging SQL statements and parameters before execution
       // 在执行前记录 SQL 语句和参数
       it.Aop.OnLogExecuting = (sql, para) =>
       {
           Console.WriteLine(UtilMethods.GetNativeSql(sql, para));
       };
   });
   return db;

已支持功能

复制代码
//查询
var list=db.Queryable<Student>().ToList();
var list2=db.Queryable<Student>().ToDataTable();
var list3= db.Queryable<Student>().Select(it=>new { 
 id=it.Id
}).ToList();  
 
//插入 、删除和更新只要是实体目前测试下来 都OK
db.Insertable(new Student()
{
    Id = 1,
    Name = "aa"
}).ExecuteCommand();
db.Deleteable(new Student()
{
    Id = 1,
    Name = "aa"
}).ExecuteCommand();
db.Updateable(new Student()
{
    Id = 1,
    Name = "aa"
}).ExecuteCommand();
 
//写sql也支持  
db.Ado.GetDataTable(sql);
db.Ado.ExecuteCommand(sql);

不支功能

复制代码
//部分库建表不支持
//动态建类不支持

AOT配置教程

创建一个带AOT的类项目

新建一个rd.xml

复制代码
<Directives>
    <Application>
        <Assembly Name="SqlSugar"  Dynamic="Required All">
         </Assembly>  
    </Application>
</Directives>

改项目文件

复制代码
<Project Sdk="Microsoft.NET.Sdk.Web">
 
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <InvariantGlobalization>true</InvariantGlobalization>
    <PublishAot>true</PublishAot>
    <Platforms>AnyCPU;x64</Platforms>
  </PropertyGroup>
 <ItemGroup>
     <RdXmlFile Include="rd.xml" />
 </ItemGroup>

RdXmlFile这个重点引用我们新建的rd.xml (这个xml要能发布出去)

相关推荐
雨浓YN7 小时前
OPC UA 通讯开发笔记 - 基于本地dll文件
windows·笔记
大强同学7 小时前
Obsidian CLI + Claude Code = 王炸组合
人工智能·windows·ai编程·cli
iceslime9 小时前
Windows10系统静音修复相关
windows·音频·修复
humors2219 小时前
AI工具合集,不定期更新
人工智能·windows·ai·工具·powershell·deepseek
Echo-J9 小时前
在 Windows 7 虚拟机上安装 VMware Tools 时遇到驱动无法安装的问题
windows
sR916Mecz15 小时前
pache Hop实战:Windows平台MySQL数据迁移的深度排错与性能调优
数据库·windows·mysql
小白电脑技术15 小时前
Windows家庭版开启原生远程桌面
windows
扑火的小飞蛾16 小时前
Windows 10 与 Kali Linux SSH 免密互信配置指南
linux·windows·ssh
Chris _data17 小时前
从宿主机(Windows)通过 NAT 模式连接虚拟机(Ubuntu)中的 MySQL
windows·mysql·ubuntu
高兴就好204818 小时前
ClaudeCode在windows 11安装方法(2026年4月)
windows