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要能发布出去)

相关推荐
j***51892 小时前
Redis 安装及配置教程(Windows)【安装】
数据库·windows·redis
Karl_wei3 小时前
桌面应用开发,Flutter 与 Electron如何选
windows·flutter·electron
未名编程6 小时前
Windows 下如何部署 Nacos 并导入配置文件
java·windows
2501_941885968 小时前
智能物流配送优化系统开发与多语言微服务实践:Python、Java、C++与Go全栈实现解析
windows
p***s919 小时前
Windows安装Rust环境(详细教程)
开发语言·windows·rust
lihui_cbdd10 小时前
幽灵卡顿:Windows 11 “数据正常但系统卡死“ 的深度排查与终极优化指南
windows
林抒10 小时前
(2025版)MongoDB 8.0.13 版本安装与配置(Windows 版)保姆级教程
windows·mongodb·nosql数据库
vortex512 小时前
Windows 下 Git Bash 终端高效配置指南
windows·git·bash
开朗觉觉13 小时前
poi导出大量数据到Excel
windows·excel
爱吃土豆的马铃薯ㅤㅤㅤㅤㅤㅤㅤㅤㅤ13 小时前
DBGPT对外API文件说明 以及 DBGPT对外API接口说明
数据库·windows·db-gpt