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

相关推荐
非凡ghost6 小时前
猫眼浏览器(Chrome内核增强版浏览器)官方便携版
前端·网络·chrome·windows·软件需求
熊文豪11 小时前
Windows安装RabbitMQ保姆级教程
windows·分布式·rabbitmq·安装rabbitmq
搬砖的小码农_Sky12 小时前
Windows操作系统上`ping`命令的用法详解
运维·网络·windows
Kiri霧19 小时前
Rust模式匹配详解
开发语言·windows·rust
程序设计实验室21 小时前
使用命令行删除 Windows 网络映射驱动器
windows
用户31187945592181 天前
Windows 电脑安装 XTerminal 1.25.1 x64 版(带安装包下载关键词)
windows
Logintern091 天前
windows如何设置mongodb的副本集
数据库·windows·mongodb
Chandler241 天前
一图掌握 操作系统 核心要点
linux·windows·后端·系统
ajassi20001 天前
开源 C# 快速开发(十七)进程--消息队列MSMQ
windows·开源·c#
Python私教1 天前
5分钟上手 MongoDB:从零安装到第一条数据插入(Windows / macOS / Linux 全平台图解)
windows·mongodb·macos