Net8 ABP VNext集成FreeSql、SqlSugar

ABP可以快速搭建开发架构,但是内置的是EFCore,国内中小企业使用FreeSql与SqlSugar还是较多,为新手提供使用提供参考

ABP、FreeSql、SqlSugar参考地址:

ABP Framework | Open source web application framework for ASP.NET Core

指南 | FreeSql 官方文档

SqlSugar .Net ORM 5.X 官网 、文档、教程 - SqlSugar 5x - .NET果糖网

打开abp官网

复制到命令行,创建解决方案,创建后如图

打开创建的解决方案

修改数据库连接配置,运行,生成数据库

添加实体与IRepository接口,如图

生成实体表,方法此处省略,请自行到官网查看文档,或者参考我的笔记(下载链接在最上面)

用Navicat Premium 连接mysql

添加SqlSugarCore

添加FreeSql.All

具体原理请参考ABP官方Dapper 集成

添加MESFreeSqlModule、FreeSqlRepository、SqlSugarRepository、MESSqlSugarModule

代码

public class MESFreeSqlModule : AbpModule
{
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        var configuration = context.Services.GetConfiguration();
        var connectionString = configuration.GetConnectionString("Default");           
        var freeSql = new FreeSql.FreeSqlBuilder()
            .UseConnectionString(FreeSql.DataType.MySql, connectionString)
            .Build();

        context.Services.AddSingleton<IFreeSql>(freeSql);
    }
}
  public abstract class FreeSqlRepository : DomainService
  {
      protected IFreeSql FreeSql => LazyServiceProvider.LazyGetRequiredService<IFreeSql>();

      private ICancellationTokenProvider CancellationTokenProvider =>
          LazyServiceProvider.LazyGetService<ICancellationTokenProvider>(NullCancellationTokenProvider.Instance);

      protected virtual CancellationToken GetCancellationToken(CancellationToken preferredValue = default)
      {
          return CancellationTokenProvider.FallbackToProvider(preferredValue);
      }
  }
    public abstract class SqlSugarRepository : DomainService
    {
        protected ISqlSugarClient SugarClient => LazyServiceProvider.LazyGetRequiredService<ISqlSugarClient>();

        private ICancellationTokenProvider CancellationTokenProvider =>
            LazyServiceProvider.LazyGetService<ICancellationTokenProvider>(NullCancellationTokenProvider.Instance);

        protected virtual CancellationToken GetCancellationToken(CancellationToken preferredValue = default)
        {
            return CancellationTokenProvider.FallbackToProvider(preferredValue);
        }
    }
public class MESSqlSugarModule : AbpModule
{
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        var configuration = context.Services.GetConfiguration();
        var connectionString = configuration.GetConnectionString("Default"); 

        context.Services.AddSingleton<ISqlSugarClient>(s =>
        {                
            SqlSugarScope sqlSugar = new SqlSugarScope(new ConnectionConfig()
            {
                DbType = SqlSugar.DbType.MySql,
                ConnectionString = connectionString,
                IsAutoCloseConnection = true,
                ConfigureExternalServices = new ConfigureExternalServices()
                {
                    EntityService = (property, column) =>
                    {
                        var attributes = property.GetCustomAttributes(true);//get all attributes 

                        if (attributes.Any(it => it is KeyAttribute))// by attribute set primarykey
                        {
                            column.IsPrimarykey = true; //有哪些特性可以看 1.2 特性明细
                        }
                        //可以写多个,这边可以断点调试
                        // if (attributes.Any(it => it is NotMappedAttribute))
                        //{
                        //    column.IsIgnore= true; 
                        //}
                    },
                    EntityNameService = (type, entity) =>
                    {
                        var attributes = type.GetCustomAttributes(true);
                        if (attributes.Any(it => it is TableAttribute))
                        {
                            var attr = (attributes.First(it => it is TableAttribute) as TableAttribute);
                            entity.DbTableName = attr.Name;
                        }
                    }
                }
            },
           db =>
           {                   

               db.Aop.OnLogExecuting = (sql, pars) =>
               {
                   
               };
           });
            return sqlSugar;
        });
    }

上面为什么加ConfigureExternalServices这段代码,参考【实体配置】实体使用自定义特性,如下图

增加Repository数据仓库,实现数据读写

同一个接口不允许同时使用FreeSql与SqlSugar,可以右建先排除掉,方便测试

添加AppService业务层

在JC.AI.MES.HttpApi.Host注入MESSqlSugarModule

启动调试即可

没有基础的可以下载我的ABP Vnext敏捷开发笔记

相关推荐

  1. .NET8 依赖注入

    2024-03-15 18:54:02       52 阅读
  2. .NET 8 网络改进

    2024-03-15 18:54:02       56 阅读
  3. Asp .Net Core 系列:Asp .Net Core 集成 Newtonsoft.Json

    2024-03-15 18:54:02       40 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-15 18:54:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-15 18:54:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-15 18:54:02       87 阅读
  4. Python语言-面向对象

    2024-03-15 18:54:02       96 阅读

热门阅读

  1. C++ 智能指针的正确使用方式:unique_ptr VS shared_ptr

    2024-03-15 18:54:02       34 阅读
  2. k8s的pod服务升级,通过部署helm升级

    2024-03-15 18:54:02       44 阅读
  3. axios 请求 url 地址,判断网络地址是否存在

    2024-03-15 18:54:02       42 阅读
  4. 面试经典-26-Z 字形变换

    2024-03-15 18:54:02       43 阅读
  5. mysql统计数据库大小

    2024-03-15 18:54:02       40 阅读
  6. TCP客户端发送结构体数据

    2024-03-15 18:54:02       39 阅读
  7. 数仓开发之ODS层

    2024-03-15 18:54:02       47 阅读
  8. 微信小程序canvas画布不清晰解决方法

    2024-03-15 18:54:02       43 阅读
  9. PyMySQL

    2024-03-15 18:54:02       42 阅读
  10. 设置docker和docker容器开机自启

    2024-03-15 18:54:02       42 阅读
  11. C语言(数组)单元练习二

    2024-03-15 18:54:02       38 阅读
  12. 有趣之matlab-烟花

    2024-03-15 18:54:02       43 阅读