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