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敏捷开发笔记

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神色自若

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值