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

相关推荐
补三补四2 小时前
操作系统:计算机世界的基石与演进
开发语言·windows·计算机外设
chuhx6 小时前
Stream API 对两个 List 进行去重操作
数据结构·windows·list
爱编程的鱼12 小时前
C# 枚举(Enum)声明与使用详解
java·windows·c#
进取星辰17 小时前
Windows 10 上运行 Ollama 时遇到 llama runner process has terminated: exit status 2
windows·llama
sukalot17 小时前
Windows同步技术-使用命名对象
windows
快乐点吧19 小时前
【Flume 】Windows安装步骤、配置环境
大数据·windows·flume
Java手札20 小时前
Windows下Golang与Nuxt项目宝塔部署指南
开发语言·windows·golang
心灵宝贝20 小时前
Postman-win64-7.2.2 安装教程(Windows 64位详细步骤)
windows·测试工具·postman
快乐点吧1 天前
【MongoDB】windows安装、配置、启动
数据库·windows·mongodb
Java-云海1 天前
微软官网Win10镜像下载快速获取ISO文件
windows·microsoft·虚拟机·iso镜像·windows10系统