当前位置:   article > 正文

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

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

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

开发环境:.NET6、Volo.Abp

数据库:Sqlite

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

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

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

  1. <Project Sdk="Microsoft.NET.Sdk">
  2. <PropertyGroup>
  3. <TargetFramework>net6.0</TargetFramework>
  4. <ImplicitUsings>enable</ImplicitUsings>
  5. <Nullable>enable</Nullable>
  6. </PropertyGroup>
  7. <ItemGroup>
  8. <PackageReference Include="Volo.Abp.AspNetCore" Version="6.0.3" />
  9. <PackageReference Include="Volo.Abp.Autofac" Version="6.0.3" />
  10. <PackageReference Include="Volo.Abp.AutoMapper" Version="6.0.3" />
  11. <PackageReference Include="Volo.Abp.Ddd.Application" Version="6.0.3" />
  12. </ItemGroup>
  13. <ItemGroup>
  14. <ProjectReference Include="..\DemoEntityFrameworkCore\DemoEntityFrameworkCore.csproj" />
  15. </ItemGroup>
  16. </Project>

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

  1. using DemoApplication.Book;
  2. using DemoEntityFrameworkCore;
  3. using Microsoft.Extensions.DependencyInjection;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using Volo.Abp.Application;
  10. using Volo.Abp.Autofac;
  11. using Volo.Abp.AutoMapper;
  12. using Volo.Abp.Modularity;
  13. namespace DemoApplication
  14. {
  15. [DependsOn(typeof(AbpAutoMapperModule), typeof(AbpAutofacModule))]
  16. public class DemoApplicationCoreModule:AbpModule
  17. {
  18. public override void ConfigureServices(ServiceConfigurationContext context)
  19. {
  20. Configure<AbpAutoMapperOptions>(options =>
  21. {
  22. options.AddMaps<DemoApplicationCoreModule>();
  23. });
  24. }
  25. }
  26. }

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

  1. using DemoDomain.Book;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Volo.Abp.Application.Services;
  8. using Volo.Abp.Autofac;
  9. using Volo.Abp.DependencyInjection;
  10. using Volo.Abp.Domain.Repositories;
  11. using Volo.Abp.Modularity;
  12. namespace DemoApplication.Book
  13. {
  14. [DependsOn(typeof(AbpAutofacModule))]
  15. public class BookInfoAppService:ApplicationService,ITransientDependency
  16. {
  17. private readonly IRepository<BookInfo, Guid> _bookInfoRepository;
  18. public BookInfoAppService(IRepository<BookInfo, Guid> bookInfoRepository)
  19. {
  20. _bookInfoRepository = bookInfoRepository;
  21. }
  22. public List<BookInfo> Get()
  23. {
  24. long count= _bookInfoRepository.GetCountAsync().Result;
  25. List<BookInfo> listBookInfos= _bookInfoRepository.GetListAsync().Result;
  26. return listBookInfos;
  27. }
  28. }
  29. }

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace DemoApplication.Book.Dto
  7. {
  8. public class BookInfoDTO
  9. {
  10. public string Name { get; set; }
  11. public string Description { get; set; }
  12. }
  13. }

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

  1. using AutoMapper;
  2. using DemoApplication.Book.Dto;
  3. using DemoDomain.Book;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using Volo.Abp.Autofac;
  10. using Volo.Abp.AutoMapper;
  11. using Volo.Abp.Modularity;
  12. namespace DemoApplication
  13. {
  14. [DependsOn(typeof(AbpAutofacModule))]
  15. public class ApplicationAutoMapperProfile:Profile
  16. {
  17. public ApplicationAutoMapperProfile()
  18. {
  19. //创建一个实体的映射
  20. CreateMap<BookInfo, BookInfoDTO>();
  21. }
  22. }
  23. }

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

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

  1. using Volo.Abp.Modularity;
  2. using Volo.Abp.Autofac;
  3. using Volo.Abp.AspNetCore;
  4. using Volo.Abp;
  5. using DemoApplication;
  6. using DemoApplication.Book;
  7. using Volo.Abp.AutoMapper;
  8. using DemoEntityFrameworkCore;
  9. using Microsoft.EntityFrameworkCore;
  10. using Volo.Abp.Auditing;
  11. using Microsoft.Extensions.DependencyInjection.Extensions;
  12. using DemoDomain;
  13. namespace DemoAspNetCoreApplict
  14. {
  15. [DependsOn(
  16. typeof(AbpAspNetCoreModule),
  17. typeof(AbpAutofacModule),
  18. typeof(DemoDomainAbpModule),
  19. typeof(DemoEntityFrameworkCroeAbpModule),
  20. typeof(DemoApplicationCoreModule))]
  21. public class DemoAbpModule:AbpModule
  22. {
  23. public override void PreConfigureServices(ServiceConfigurationContext context)
  24. {
  25. base.PreConfigureServices(context);
  26. var hostingEnviroment = context.Services.GetHostingEnvironment();
  27. var configuration = context.Services.GetConfiguration();
  28. }
  29. public override void OnApplicationInitialization(ApplicationInitializationContext context)
  30. {
  31. var app=context.GetApplicationBuilder();
  32. var env=context.GetEnvironment();
  33. if(env.IsDevelopment())
  34. {
  35. app.UseExceptionHandler("/Error");
  36. app.UseHsts();
  37. }
  38. app.UseHttpsRedirection();
  39. app.UseStaticFiles();
  40. app.UseRouting();
  41. app.UseEndpoints(endpoints =>
  42. {
  43. endpoints.MapControllers();
  44. });
  45. app.UseConfiguredEndpoints();
  46. //app.UseAuthorization();
  47. base.OnApplicationInitialization(context);
  48. }
  49. public override void ConfigureServices(ServiceConfigurationContext context)
  50. {
  51. base.ConfigureServices(context);
  52. context.Services.AddControllers();
  53. context.Services.AddDbContext<DemoDbContext>(options =>
  54. {
  55. options.UseSqlite("Data Source=E:\\ABP\\demo.db;");
  56. });
  57. Configure<AbpAutoMapperOptions>(options =>
  58. {
  59. options.AddMaps<DemoApplicationCoreModule>();
  60. });
  61. context.Services.Configure<AbpAuditingOptions>(options =>
  62. {
  63. options.IsEnabled = true;
  64. });
  65. }
  66. }
  67. }

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

  1. typeof(DemoDomainAbpModule),
  2. typeof(DemoEntityFrameworkCroeAbpModule),
  3. typeof(DemoApplicationCoreModule)

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

  1. using DemoApplication.Book;
  2. using DemoApplication.Book.Dto;
  3. using DemoDomain.Book;
  4. using Microsoft.AspNetCore.Mvc;
  5. using System.Collections.Generic;
  6. using Volo.Abp.AspNetCore.Mvc;
  7. namespace DemoAspNetCoreApplict
  8. {
  9. [ApiController]
  10. [Route("api/[controller]/[action]")]
  11. public class GetBookInfoController: AbpController
  12. {
  13. private readonly ILogger<GetBookInfoController> _logger;
  14. private readonly BookInfoAppService _bookInfoAppService;
  15. public GetBookInfoController(ILogger<GetBookInfoController> logger, BookInfoAppService bookInfoAppService)
  16. {
  17. _logger = logger;
  18. _bookInfoAppService = bookInfoAppService;
  19. }
  20. [HttpGet]
  21. public IEnumerable<BookInfo> Get()
  22. {
  23. List<BookInfo> bookInfo = _bookInfoAppService.Get().ToList();
  24. return bookInfo;
  25. }
  26. }
  27. }

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

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

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/141366
推荐阅读
相关标签
  

闽ICP备14008679号