ABP入门教程(五)集成Dapper
如果在项目中要直接写T-SQL,可以引入Dapper
1,在EntityFrameworkCore层和Application层添加引用: Abp.Dapper
2,在EntityFrameworkModule类中添加两句代码就完成了配置
//1 在DependsOn中添加
typeof(AbpDapperModule)
//2 在 Initialize()方法中添加
DapperExtensions.DapperExtensions.SetMappingAssemblies(new List<Assembly> { typeof(ABP7VueEntityFrameworkModule).GetAssembly() });
最终效果如下
namespace ABP7Vue.EntityFrameworkCore
{[DependsOn(typeof(ABP7VueCoreModule), typeof(AbpZeroCoreEntityFrameworkCoreModule),typeof(AbpDapperModule))]public class ABP7VueEntityFrameworkModule : AbpModule{/* Used it tests to skip dbcontext registration, in order to use in-memory database of EF Core */public bool SkipDbContextRegistration { get; set; }public bool SkipDbSeed { get; set; }public override void PreInitialize(){if (!SkipDbContextRegistration){Configuration.Modules.AbpEfCore().AddDbContext<ABP7VueDbContext>(options =>{if (options.ExistingConnection != null){ABP7VueDbContextConfigurer.Configure(options.DbContextOptions, options.ExistingConnection);}else{ABP7VueDbContextConfigurer.Configure(options.DbContextOptions, options.ConnectionString);}});}}public override void Initialize(){IocManager.RegisterAssemblyByConvention(typeof(ABP7VueEntityFrameworkModule).GetAssembly());DapperExtensions.DapperExtensions.SetMappingAssemblies(new List<Assembly> { typeof(ABP7VueEntityFrameworkModule).GetAssembly() });}public override void PostInitialize(){if (!SkipDbSeed){SeedHelper.SeedHostDb(IocManager);}}}
}
3,在服务中注入Dapper的仓储就可以使用了
public class DepartmentAppService : ABP7VueAppServiceBase
{private readonly IDapperRepository<Department, int> _DapperRepository;public DepartmentAppService(IDapperRepository<Department, int> dapperRepository){_DapperRepository = dapperRepository;}public List<Department> GetDepartmentList(){var a = new Department() { Code="Code0", Name="Name0", };_DapperRepository.Insert(a);_DapperRepository.Insert(new Department() { Code="Code1", Name="Name1", });_DapperRepository.Execute("INSERT INTO Department(Code, [Name], CreationTime, IsDeleted)VALUES ('Code', 'Name', getdate(), 0)");var entity = _DapperRepository.Query("select * from Department").ToList();return entity;}
}
在ABP项目中实现自定义仓储举例
//在Core层添加仓储接口
public interface IXxxxxRepository
{Task<int> DoSomethingAboutDb(List<long> input, long userId);
}//在EntityFrameworkCore层添加仓储实现, 继承Dapper仓储基类DapperEfRepositoryBase , 第一参数为DbContext, 第二参数为实体, 第三个参数为主键类型
public class XxxxxRepository : DapperEfRepositoryBase<EpiDbContext, Lab, long>, IXxxxxRepository, ITransientDependency
{ILogger Logger { get; set; }string _connectionStrings;public XxxxxRepository(IActiveTransactionProvider activeTransactionProvider, IHostingEnvironment env) : base(activeTransactionProvider){var _appConfiguration = AppConfigurations.Get(env.ContentRootPath, env.EnvironmentName, env.IsDevelopment());_connectionStrings = _appConfiguration["ConnectionStrings:Default"];}public async Task<int> DoSomethingAboutDb(List<long> input, long userId){//创建临时表var sqlCreate = @"CREATE TABLE #expId(Id BIGINT); ";//插入数据到临时表var sqlInsert = "";foreach (long expId in input){sqlInsert += $" INSERT INTO #expId(Id) VALUES ({@expId}); ";}//执行存储过程var sqlSp = " exec spDoSomethingAboutDb @userId=@userId";string sql = sqlCreate + sqlInsert + sqlSp;return await ExecuteAsync(sql, new { userId = userId });}
}
然后在应用层或core层直接注入此仓储就可以使用了
ABP入门教程(五)集成Dapper相关推荐
- ABP入门教程之集成达梦数据库
达梦数据库目前(2023年)只支持到.net6 所以下载ABP的.net6版本,然后开始集成 1,修改数据库连接 首先在达梦数据库中新建一个用户:ABPDEMO,赋予DBA权限,然后把appsetti ...
- ABP(现代ASP.NET样板开发框架)系列之2、ABP入门教程
点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之2.ABP入门教程 ABP是"ASP.NET Boilerplate Project (ASP.NET样 ...
- Scrapy爬虫入门教程五 Selectors(选择器)
Scrapy爬虫入门教程一 安装和基本使用 Scrapy爬虫入门教程二 官方提供Demo Scrapy爬虫入门教程三 命令行工具介绍和示例 Scrapy爬虫入门教程四 Spider(爬虫) Scrap ...
- sql数据库教程百度云_绘画自学零基础入门教程|五天学会绘画pdf百度云下载!...
绘画自学零基础入门教程|五天学会绘画pdf百度云下载!画画是可以让人留存记忆的事情.我自己就很喜欢画画来记录生活中一些特别的日子.场景还有我的家人朋友们.有时候,比照片更有故事感和纪念意义-有空拿出来 ...
- R语言七天入门教程五:认识并使用函数
R语言七天入门教程五:认识并使用函数 一.什么是函数 在编程语言中,如果有一段代码需要在多次重复使用,除了复制粘贴外,还可以将其写成一个函数.函数可以很方便地实现代码复用,对于复杂的程序功能,可以将其 ...
- linux宝塔搭建网站,宝塔Linux面板搭建网站入门教程五(宝塔Linux面板安装WordPress博客程序)...
本文于 2021-04-01 00:04 更新,本文內容具有時效性,如有失效,請在博客站內搜相關商家名稱,即可查看到關於此商家的最新優惠活動! 國外商家建議使用"谷歌瀏覽器"訪問, ...
- SpringCloud 入门教程(五): Ribbon实现客户端的负载均衡
接上节,假如我们的Hello world服务的访问量剧增,用一个服务已经无法承载, 我们可以把Hello World服务做成一个集群. 很简单,我们只需要复制Hello world服务,同时将原来的端 ...
- Android入门教程五之使用AndroidStudio+SDK开发安卓APP
<!-- Android 基础入门教程 end --><div class="col middle-column big-middle-column">&l ...
- python画图marker显示_python画图(标记、marker、设置标记大小、marker符号大全)(图文详细入门教程五)...
初衷 本人由于平常写论文需要输出一些结果图,但是苦于在网上搜python画图时,详细的教程非常多,但是就是找不到能马上解决自己问题那一行代码,所以打算写一些适合需求简单的朋友应急用的教程,应急就必须方 ...
最新文章
- Windows客户端C/C++编程规范“建议”——前言
- Python第三章-字符串
- 云原生时代,Java还是Go?
- linux数据库怎么import,mysqlimport
- Centos7 安装 Rabbitmq、Erlang
- MFC工作笔记0002---MFC HelloWorld程序
- Head first 第一章
- ImportError: libcudart.so.9.2: cannot open shared object file: No such file or directory
- import matplotlib.pyplot as plt
- 计算机cmd管理员,cmd获取管理员权限的命令是什么
- 码~ps基本概念知识
- Windows用户账户控制详解
- 自己动手写java虚拟机(第一话)
- java tld tag_自定义标签tag(jsp编写)和tld(java编写)的使用方法
- 2DPCA的原理推导与实现
- 传统企业如何实现数字化转型
- 仪表板工具Stimulsoft Dashboards中的文本元素介绍
- 【数据集|COCO】COCO格式数据集制作与数据集参数计算
- 贵阳哪里有计算机二级培训机构,贵阳省计算机二级考试培训
- 华为交换机:三层交换机实现vlan间通信