当前位置: 首页 > article >正文

使用Volo.Abp读取Sqlite表中数据

书接上文:Abp 从空白的WebApplication中添加EntityFrameworkCore生成数据库

开发环境:.NET6、Volo.Abp

数据库:Sqlite

说明:纯属个人强行入门。我个人觉得按照官网的操作不舒服,所以自己研究着来,请读者根据自己的需要进行参考。我能保证的是按照文章操作能够得到和我一样的结果。

1、在应用层中添加项目DemoApplication 项目目录如下图所示:

1.1、项目中引入包情况如下:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Volo.Abp.AspNetCore" Version="6.0.3" />
    <PackageReference Include="Volo.Abp.Autofac" Version="6.0.3" />
    <PackageReference Include="Volo.Abp.AutoMapper" Version="6.0.3" />
    <PackageReference Include="Volo.Abp.Ddd.Application" Version="6.0.3" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\DemoEntityFrameworkCore\DemoEntityFrameworkCore.csproj" />
  </ItemGroup>

</Project>

1.2、创建DemoApplicationCoreModule类,代码如下:

using DemoApplication.Book;
using DemoEntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application;
using Volo.Abp.Autofac;
using Volo.Abp.AutoMapper;
using Volo.Abp.Modularity;

namespace DemoApplication
{
    [DependsOn(typeof(AbpAutoMapperModule), typeof(AbpAutofacModule))]
    public class DemoApplicationCoreModule:AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {           
            Configure<AbpAutoMapperOptions>(options =>
            {
                options.AddMaps<DemoApplicationCoreModule>();
            });
        }
    }
}

1.3、创建类BookInfoAppService,代码如下:

using DemoDomain.Book;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Autofac;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Modularity;

namespace DemoApplication.Book
{
    [DependsOn(typeof(AbpAutofacModule))]
    public class BookInfoAppService:ApplicationService,ITransientDependency
    {
        private readonly IRepository<BookInfo, Guid> _bookInfoRepository;

        public BookInfoAppService(IRepository<BookInfo, Guid> bookInfoRepository)
        {
            _bookInfoRepository = bookInfoRepository;
        }

        public List<BookInfo> Get()
        {
           long count= _bookInfoRepository.GetCountAsync().Result;

           List<BookInfo> listBookInfos= _bookInfoRepository.GetListAsync().Result;

           return listBookInfos;
        }
    }
}

1.4、目录中BookInfoDTO类目前没有使用到,也把代码粘上来吧:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DemoApplication.Book.Dto
{
    public class BookInfoDTO
    {
        public string Name { get; set; }
        public string Description { get; set; }
    }
}

1.5、 目录中ApplicationAutoMapperProfile类目前没有使用到,也把代码粘上来吧:

using AutoMapper;
using DemoApplication.Book.Dto;
using DemoDomain.Book;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Autofac;
using Volo.Abp.AutoMapper;
using Volo.Abp.Modularity;

namespace DemoApplication
{
    [DependsOn(typeof(AbpAutofacModule))]
    public class ApplicationAutoMapperProfile:Profile
    {
        public ApplicationAutoMapperProfile()
        {
            //创建一个实体的映射
            CreateMap<BookInfo, BookInfoDTO>(); 
        }
    }
}

2、对于用户接口层要做的修改。

2.1、修改DemoAbpModule类中代码,具体代码如下:

using Volo.Abp.Modularity;
using Volo.Abp.Autofac;
using Volo.Abp.AspNetCore;
using Volo.Abp;
using DemoApplication;
using DemoApplication.Book;
using Volo.Abp.AutoMapper;
using DemoEntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.Auditing;
using Microsoft.Extensions.DependencyInjection.Extensions;
using DemoDomain;

namespace DemoAspNetCoreApplict
{
    [DependsOn(
        typeof(AbpAspNetCoreModule),
        typeof(AbpAutofacModule),
        typeof(DemoDomainAbpModule),
        typeof(DemoEntityFrameworkCroeAbpModule),
        typeof(DemoApplicationCoreModule))]

    public class DemoAbpModule:AbpModule
    {
        public override void PreConfigureServices(ServiceConfigurationContext context)
        {
            base.PreConfigureServices(context);

            var hostingEnviroment = context.Services.GetHostingEnvironment();
            var configuration = context.Services.GetConfiguration();

        }
        public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {         
            var app=context.GetApplicationBuilder();
            var env=context.GetEnvironment();

            if(env.IsDevelopment())
            {
                app.UseExceptionHandler("/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseRouting();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
            app.UseConfiguredEndpoints();
            //app.UseAuthorization();

            base.OnApplicationInitialization(context);
        }

        public override void ConfigureServices(ServiceConfigurationContext context)
        {          
            base.ConfigureServices(context);

            context.Services.AddControllers();

            context.Services.AddDbContext<DemoDbContext>(options =>
            {
                options.UseSqlite("Data Source=E:\\ABP\\demo.db;");
            });

            Configure<AbpAutoMapperOptions>(options =>
            {
                options.AddMaps<DemoApplicationCoreModule>();
            });

            context.Services.Configure<AbpAuditingOptions>(options =>
            {
                options.IsEnabled = true;
            });

        }
    }
}

2.2、注意需要添加依赖对象,不然你会收到意想不到的意外哦。具体如下:

        typeof(DemoDomainAbpModule),
        typeof(DemoEntityFrameworkCroeAbpModule),
        typeof(DemoApplicationCoreModule)

2.3、添加一个控制器GetBookInfoController,具体代码如下:

using DemoApplication.Book;
using DemoApplication.Book.Dto;
using DemoDomain.Book;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using Volo.Abp.AspNetCore.Mvc;

namespace DemoAspNetCoreApplict
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class GetBookInfoController: AbpController
    {
        private readonly ILogger<GetBookInfoController> _logger;

        private readonly BookInfoAppService _bookInfoAppService;

        public GetBookInfoController(ILogger<GetBookInfoController> logger, BookInfoAppService bookInfoAppService)
        {
            _logger = logger;
            _bookInfoAppService = bookInfoAppService;
        }

        [HttpGet]
        public IEnumerable<BookInfo> Get()
        {
            List<BookInfo> bookInfo = _bookInfoAppService.Get().ToList();

            return bookInfo;
        }
    }
}

3、具体的可执行代码已上传,想赚点小钱钱喽有需要的请去下载吧。

https://download.csdn.net/download/xingchengaiwei/888151984

4、推广一个云服务器,买服务器的私聊我送源代码呦。

开发云 - 一站式云服务平台 


http://www.kler.cn/a/230154.html

相关文章:

  • iframe温习+应用
  • React第七节 组件三大属性之 refs 的用法注意事项
  • SpringBoot集成Minio实现上传凭证、分片上传、秒传和断点续传
  • <项目代码>YOLOv8 航拍行人识别<目标检测>
  • vulnhub靶场之corrosion靶场1
  • 如何使用GCC手动编译stm32程序
  • 关联语句join与合并语句union
  • vue - 指令(一)
  • CGAL::2D Arrangements-3
  • @ 代码随想录算法训练营第7周(C语言)|Day41(动态规划)
  • Redis核心技术与实战【学习笔记】 - 23.Redis 主从切换故障,有哪些坑
  • UML之在Markdown中使用Mermaid绘制类图
  • #Z1103. good point
  • 如何使用 Bard 中的画图功能
  • 生存类游戏《幻兽帕鲁》从部署服务器到开始体验全过程
  • ArcGIS学习(三)数据可视化
  • python--第三方包的使用
  • MySQL查询优化技巧和10个案例展示
  • 开启一个服务,将服务器指定的文件读取,传播到网上其他终端
  • Mysql报错:too many connections
  • Ubuntu修改用户名及密码
  • 项目02《游戏-07-开发》Unity3D
  • 浅谈bypass Etw
  • Spring-mvc、Spring-boot中如何在调用同类方法时触发AOP
  • 灵活应对:策略模式在软件设计中的应用
  • 析构函数