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

相关推荐
集成显卡20 小时前
windows 下使用 bat 批处理运行 Chrome 无头模式刷一波访问量
windows·程序员
路由侠内网穿透3 天前
本地部署 GPS 跟踪系统 Traccar 并实现外部访问
运维·服务器·网络·windows·tcp/ip
研华嵌入式4 天前
如何在高通跃龙QCS6490 Arm架构上使用Windows 11 IoT企业版?
arm开发·windows·嵌入式硬件
带娃的IT创业者4 天前
Windows 平台上基于 MCP 构建“文心一言+彩云天气”服务实战
人工智能·windows·文心一言·mcp
csdn_aspnet4 天前
Windows Node.js 安装及环境配置详细教程
windows·node.js
摇滚侠4 天前
java语言中,list<String>转成字符串,逗号分割;List<Integer>转字符串,逗号分割
java·windows·list
Source.Liu4 天前
【Pywinauto库】12.2 pywinauto.element_info 后端内部实施模块
windows·python·自动化
Source.Liu4 天前
【Pywinauto库】12.1 pywinauto.backend 后端内部实施模块
开发语言·windows·python·自动化
私人珍藏库4 天前
[Windows] FileOptimizer v17.1.0_一款文件批量压缩工具
windows·批量压缩
掘根4 天前
【CMake】List
windows·microsoft·list